I am trying to take a string input from the user (with JOptionPane) and check to see if the reverse string "equals"(using String, StringBuffer, and StringTokenizer Classes). I believe I need to elliminate the spaces first, but I cannot figure out how to do this?
This is a very common homework problem which many Ranchers probably have done before. (If not, any programmer with some bit of experience can probably figure it out.) My point is that it doesn't help you much if we do it for you. So what have you done so far? Have you even started any code yet? You probably should look at the Java API documentation to see what methods are available with String and StringBuffer.
When you get a good start, post some code here and try to explain what you are stuck on. We'll be more than happy to help from there.
As much as I would like to post the method I just wrote, Layne is right: That wouldn't really help you.
But if you post the code you've written so far -- or tell us specifically what you're stuck on -- I would be happy to offer some hints.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Sep 06, 2004
That is all right. You are right. I figured out what I was doing right after I posted the question. Some time it helps just to write down what you are trying to do, and when you get back to the program, something jumps out at you. The code I used was:
StringTokenizer st = new StringTokenizer(s1, ",.?;:'! -"); int i = st.countTokens(); System.out.println(i); StringBuffer strBuf = new StringBuffer(); for (i = st.countTokens(); i > 0; i--) strBuf.append(st.nextToken());
And, then used the reverse method in the StringBuffer class.
Now, this works for the specific examples that I am trying to determine whether they are palindromes, or not. However, using my String delim (e.g. ",.?' -") to elliminate non-characters does not seem very elligant (to me)? I would like to use something like "!= char", but (althogh I have tried some options) I cannot seem to figure this part out. Is this a specific enough question that you can provide some hints?
Originally posted by Mitch Krah: However, using my String delim (e.g. ",.?' -") to elliminate non-characters does not seem very elligant (to me)? I would like to use something like "!= char", but (althogh I have tried some options) I cannot seem to figure this part out. Is this a specific enough question that you can provide some hints?
After I read your original question, I decided to write this program myself. I didn't deal with punctuation marks, but I was planning on implementing it the same way you have here. I agree that this doesn't seem very elligant(sp?), either, but using an if statement to compare char by char would be much, much more work. I'm not certain if I have a satisfactory answer at the moment.
Joined: Dec 06, 2001
Hmm...Perhaps regular expressions can provide a more elegant way to remove characters, but this is probably more advanced than you need for this particular concept.