File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More String Questions

 
Mitch Krah
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mitch Krah
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mitch Krah:
...I would like to use something like "!= char"...

!Character.isLetter(char)
-or-
!Character.isLetterOrDigit(char)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic