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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.