Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4238266

StringTokenizer.hasMoreElements() has unexpected side-effect

XMLWordPrintable

    • 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)
      --------------
      ======================================================================

            nsanjeevsunw Naveen Sanjeeva (Inactive)
            gorsunw Gor Gor (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: