DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Threaded View

  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

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