DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008

    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
    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("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)
        //Fixes any errors in the input string
        public static String fixErrors(String tempString)
        Pattern pattern = Pattern.compile("[^@][@?+]");
        Matcher matcher = pattern.matcher(tempString);
            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
    Sterling Heights, Michigan
    Does the code you have work they way you need it to?

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

  4. #4
    Join Date
    Jul 2005
    SW MO, USA
    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

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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.