-
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
-
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
-
ya it does...but i wanted to try it another way...hoping for help though
-
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
-
By Kallahan in forum Java
Replies: 1
Last Post: 01-20-2003, 08:28 AM
-
Replies: 0
Last Post: 02-22-2002, 03:45 PM
-
By Charlene in forum Java
Replies: 2
Last Post: 08-07-2000, 07:03 AM
-
By Muhammad Alam in forum Java
Replies: 1
Last Post: 03-24-2000, 09:19 AM
-
By shalom cohen in forum Java
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
Forum Rules
|
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
|
Bookmarks