Ondrej Tokar
Ondrej Tokar

Reputation: 5070

Why does StringTokenizer.hasMoreTokens return true when it shouldn't?

I have this code:

StringTokenizer st = new StringTokenizer (line, String.valueOf(delimiter));

labels.add("Time");

int currentCol = 1;                                                 
while (st.hasMoreTokens())
{
  st.nextToken();
  labels.add(new String("c" + currentCol++));                                
}

Where delimiter is ; and line is 0.6245.

Why do I end up with an ArrayList labels that has it's second element c1? While the documentation says:

Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.

Returns:

true if and only if there is at least one token in the string after the current position; false otherwise.

Since value: 0.6245 cannot be split by delimiter ; why would hasMoreTokens() return true?

EDIT:

Now I understand why while loop get's to execute at least once. It is because that is the way StringTokenizer works. Rather than regular split that I am used to where I can access any position of the resulting array, here the only way to access elements is with the nextElement() method. Which means that even if there is only one element in the StringTokenizer it will always return true from hasMoreTokens().

The reason why I am wondering about StringTokenizer which is what I learned a legacy class is that I am reviewing an old code that I am about to reuse.

Upvotes: 1

Views: 1392

Answers (2)

smruti ranjan
smruti ranjan

Reputation: 781

0.6245 is itself a token though you don't have a delimiter. If you would have something like this "0.6245;0.987" then it has 2 tokens. Unless your line has an empty space or a null value while loop will be entered at least once.

Upvotes: 2

Jorge Campos
Jorge Campos

Reputation: 23361

Your problem is because you are adding an element on your list outside your loop:

labels.add("Time");

You have just one token after the Tokenize works. That's because when you tokenize a String that hasn't the informed delimiter you have only one element, so if your string is 0.6245 understand it as tokens as (for your delimiter):

First token: 0.6245
Second token: nothing

It is like if it was: 0.6245;

Inside your loop you are adding another thing to your array list:

labels.add(new String("c" + currentCol++));

Since you initialized currentCol as 1 and the while will run just once it will have the c1 as value on the second element of your list (remember, you have added one before)

I don't know what are you trying to achieve. But it seems like you want something like:

StringTokenizer st = new StringTokenizer (line, String.valueOf(delimiter));

//Don't add this:
//labels.add("Time");

int currentCol = 1;                                                 
while (st.hasMoreTokens())
{
    String someThing = st.nextToken();
    labels.add(new String("Current token:" + someThing 
                   + "\n currentCol: " + currentCol++));                                
}

Upvotes: 2

Related Questions