aspose file tools*
The moose likes Beginning Java and the fly likes More String Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "More String Questions" Watch "More String Questions" New topic
Author

More String Questions

Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
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?

Any suggestions?

Thank you,
Mitch
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne
[ October 16, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
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());

System.out.println(strBuf);

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?

Thank you,
Mitch
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"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."

Absolutely. If you just say it out loud instead of writing it, I think you described the Rubber Duck Partner


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mitch Krah:
...using my String delim (e.g. ",.?' -") to eliminate non-characters does not seem very elegant (to me)? I would like to use something like "!= char"...

String has a replaceAll method that takes a regex as its first argument. You can use the regex to find anything that's not a letter (for example), then just replace it with an empty string "".



Here's a tutorial on regular expressions:
http://java.sun.com/docs/books/tutorial/extra/regex/index.html

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mitch Krah:
...I would like to use something like "!= char"...

!Character.isLetter(char)
-or-
!Character.isLetterOrDigit(char)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More String Questions