Deleting whitespaces and junk within a sentence
I'm trying to write a palindrome program, where the user input the word/sentence which may includes characters/numbers. .I have to write string method to remove the whitespaces and junk from the sentence before I can do other things. We only learned about trim, ltrim and rtrim. Can someone help me point out my mistakes and how to fix this program below?. Any helps is highly appreciated.
import java.util.*;
public class Palindrome
{
public static void main(String args[])
{
String pal;
Scanner sc = new Scanner(System.in);
for(;;)
{
String E = "E";
System.out.print("Type your Palindrome or E to Exit:");
pal = sc.nextLine();
if(pal.equalsIgnoreCase(E)) break;
String palc = pal;
}
}
//**************compressIt()******************************
private static String compressIt(String palc)
{
char c;
for(int i=0 ; i < palc.length() ; i++)
{
c = palc.charAt(i);
if(! Character.isWhitespace(c)) break;
}
return(palc.substring(0));
}
//**************removeJunk()******************************
private static String removeJunk(String palc)
{
char c;
for(int i=0 ; i < palc.length() ; i++)
{
c = palc.charAt(i);
if(! Character.isLetter(c));
}
return(palc.substring(0));
[1417 byte] By [
aivon1sta] at [2007-11-27 8:40:03]

1) Use code tags. Your code is very difficult to read. See:
http://forum.java.sun.com/help.jspa?sec=formatting
2) I'm not a fan of for (;;) loops. I prefer more logical loops like while:
boolean done = false;
while (!done)
{
String E = "E";
System.out.print("Type your Pallindrome or E to Exit:");
pal = sc.nextLine();
if (pal.equalsIgnoreCase(E))
{
done = true;
}
else
{
String palc = pal;
}
}
3) There's probably a regex way to solve this, but I'm still learning regex. Even without regex, it can't be that hard to step through the string removing any whitespace and copying only valid characters to the result string, can it? Keep at it.
We haven't learned about 'replaceall' yet, we can't use it. :(.
import java.util.*;
public class Palindrome
{
public static void main(String args[])
{
String pal;
Scanner sc = new Scanner(System.in);
for(;;)
{
String E = "E";
System.out.print("Type your Palindrome or E to Exit:");
pal = sc.nextLine();
if(pal.equalsIgnoreCase(E)) break;
String palc = pal;
}
}
//**************compressIt()******************************
private static String compressIt(String palc)
{
char c;
for(int i=0 ; i < palc.length() ; i++)
{
c = palc.charAt(i);
if(! Character.isWhitespace(c)) break;
}
return(palc.substring(0));
}
//**************removeJunk()******************************
private static String removeJunk(String palc)
{
char c;
for(int i=0 ; i < palc.length() ; i++)
{
c = palc.charAt(i);
if(! Character.isLetter(c));
}
return(palc.substring(0));
> str = str.replaceAll("\\P{Alpha}+",
> "");
> will replace all occurences of one or more
> non-alphabet characters with nothing.
Aha, I knew there must be an easy Regex solution. But again, your solution will just require you to walk through your string and copy over clean characters. Again, this should be easy to do. Give it a go, and let's see your result.
Oh, and thanks for the code formatting.
> We haven't learned about 'replaceall' yet, we can't
> use it. :(.
Create a new StringBuilder.
Iterate over the characters in your original string.
For each character, if it is in a-z or A-Z, then append it to he StringBuilder.
You now have your original string without any non-alphabet characters.
jverda at 2007-7-12 20:38:20 >
