DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    1

    java programming question

    So i was wondering...i wrote the following code using regular expressions and i wanted to know if it could be done using substring() and indexOf methods...here is my code.....

    Occasionally, backspaces in text are not handled properly, and appear as part of
    a String. This question asks you to handle the backspaces in a String. Your input
    will be a string which contains upper and lower case letters, spaces and
    punctuation, as well as the additional symbol “@”. This symbol represents that
    the user typed in a backspace, and that the previous character should be
    ignored. So for instance, if the user types in Hej@llo..output would be Hello
    Code:
    import javax.swing.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    public class A3Q3
    {
        public static void main(String [] args)
             {
        String input;                                                    //The main method of this program only contains the input string which get's
        for(input = getString("Enter a string"); checkForError(input); input = getString("Enter a string"))    //it's value from the method 'getString'. It then
        {                                                                //
            printOutput(fixErrors(input));
        }
        printOutput("This string is error free");
            }
        //Returns true if specified string has an error ('@' in it) and false
        //if it does not.
        public static boolean checkForError(String input)
        {
            Pattern pattern = Pattern.compile("@");
            Matcher matcher = pattern.matcher(input);
    
            return matcher.find();
        }
    
        //Prompts the user for a string with the specified input
        //Returns the input string
        public static String getString(String input)
        {
            String tempString = JOptionPane.showInputDialog(input);
            return tempString;
        }
    
        //Prints the specified message
        public static void printOutput(String tempString)
        {
            System.out.println(tempString);
        }
    
        //Fixes any errors in the input string
        public static String fixErrors(String tempString)
            {
        Pattern pattern = Pattern.compile("[^@][@?+]");
        Matcher matcher = pattern.matcher(tempString);
        
        while(matcher.find())
        {
            tempString = matcher.replaceAll("");
            matcher = pattern.matcher(tempString);
        }
    
        pattern = Pattern.compile("@");                //This block gets rid of any extra @'s left over.
        matcher = pattern.matcher(tempString);
        tempString = matcher.replaceAll("");
    
        return tempString;
          }
    }
    Last edited by Hack; 11-07-2008 at 07:15 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Does the code you have work they way you need it to?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Nov 2008
    Posts
    1
    ya it does...but i wanted to try it another way...hoping for help though

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Yes, it could be done with indexOf and substring. Use indexOf to find the @ then substring to pick the parts of the string you want to keep.

Similar Threads

  1. initial Java programming
    By Kallahan in forum Java
    Replies: 1
    Last Post: 01-20-2003, 08:28 AM
  2. Java error handling question
    By Tim in forum Java
    Replies: 0
    Last Post: 02-22-2002, 03:45 PM
  3. Replies: 2
    Last Post: 08-07-2000, 07:03 AM
  4. About Java programming courses in Chicago/Suburbs
    By Muhammad Alam in forum Java
    Replies: 1
    Last Post: 03-24-2000, 09:19 AM
  5. Replies: 0
    Last Post: 03-18-2000, 01:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links