-
Bug
-
Resolution: Fixed
-
P4
-
1.2.2
-
kestrel
-
sparc
-
solaris_2.5
-
Verified
Name: mgC56079 Date: 05/14/99
StringTokenizer.hasMoreElements() and StringTokenizer.hasMoreTokens() methods
have unexpected side-effect - object's internal state is changed after calling these
methods which is not reflected in the specification:
------------------------------------------------------
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.
------------------------------------------------------
Following example demonstrates that the internal state of the object has been changed while
calling hasMoreTokens()
--------------Test.java
import java.util.*;
public class Test {
public static void main(String[] argv) {
StringTokenizer st1 = new StringTokenizer("ab", "b");
System.out.println(st1.nextToken());
System.out.println(st1.nextToken(""));
StringTokenizer st2 = new StringTokenizer("ab", "b");
System.out.println(st2.nextToken());
st2.hasMoreTokens();
System.out.println(st2.nextToken(""));
}
}
--------------Output
a
b
a
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java)
at java.util.StringTokenizer.nextToken(StringTokenizer.java)
at Test.main(Test.java:12)
--------------
======================================================================
- relates to
-
JDK-4338282 StringTokenizer behavior different in latest JDK1.3
- Closed