0

Hello All,

I am using a stringtokenizer to divide a string while comparing each word in the string to a specific word and pushing it onto a stack. I can push it onto the stack with no problems; however, I cannot get it to find the specific word.

for Example:

String a = "I am here and there I go."

I need to find the word "there" in order to proceed with the rest of my program.


Here is a snippet of my code:

StringTokenizer temp = new StringTokenizer(a);
         while(temp.hasMoreTokens())
         {
         if(!temp.nextElement().equals("there"))
            stack.push(temp.nextElement());
         }

Any assistance will be appreciated.

2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by jwenting
0

seems logical, doesn't it?
If token x is not equal to "there", you push token x+1 onto the stack and continue with token x+2 if it exists.

So you check if "and" is equal to "there", push "there" onto the stack, and check if there's another token after "there".

Which will of course cause massive problems, as you may end up trying to push a token that doesn't exist, causing temp.nextElement() to fail with an Exception.

P.S. don't use StringTokenizer, it's deprecated. Use String.split() instead.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.