1 2 Previous Next 19 Replies Latest reply on Mar 3, 2005 4:06 PM by 807597

    NoSuchElementException in StringTokenizer

    807597
      hi there guys i have this exception when sorting the StringTokenizers this is my code...
      public static StringTokenizer[] bubbleSort(StringTokenizer[] s){
               StringTokenizer temp;
               for(int pass=0;pass<s.length-1;pass++){
                    for(int index=0;index<s.length-1;index++){
                         if(compare(s[index],s[index+1])){
                              temp = s[index];
                              s[index] = s[index+1];
                              s[index+1] = temp;
                         }
                    }
               }
               return s;
          }
          
          public static boolean compare(StringTokenizer date1, StringTokenizer date2){
              date1.nextToken();date1.nextToken();
              String h = date1.nextToken();
              date2.nextToken();date2.nextToken();
              String k = date2.nextToken();
              if(h.compareTo(k)<0)
                  return true;
              else return false;
          }
      they are used simultaneously so it affects one another

      this is the error:
      Exception in thread "main" java.util.NoSuchElementException
      at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)

      What seems to be the problem?
        • 1. Re: NoSuchElementException in StringTokenizer
          807597
          From StringTokenizer docs:
          nextToken

          public String nextToken(String delim)

              Returns the next token in this string tokenizer's string. First, the set of characters considered to be delimiters by this StringTokenizer object is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token. The new delimiter set remains the default after this call.

              Parameters:
                  delim - the new delimiters.
              Returns:
                  the next token, after switching to the new delimiter set.
              Throws:
                  NoSuchElementException - if there are no more tokens in this tokenizer's string.
          No more tokens.

          RD-R
          � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
          • 2. Re: NoSuchElementException in StringTokenizer
            807597
            but the line has tokens....
            this is an example of the StringTokenizer in the array:

            trans 123 12-03-2004 credit 100.00

            i want to get the date out of the StringTokenizer hence i use the nextToken method.

            the error is thrown in the compare line...
            • 3. Re: NoSuchElementException in StringTokenizer
              796447
              but the line has tokens....
              this is an example of the StringTokenizer in the
              array:

              trans 123 12-03-2004 credit 100.00

              i want to get the date out of the StringTokenizer
              hence i use the nextToken method.

              the error is thrown in the compare line...
              It looks to me that since you are calling compare in a loop, that you are probably ending up calling nextToken() on the same StringTokenizer instances over and over again, hence the error. You'd have to redesign - can't just keep using the same StringTokenizer over and over again without somehow resetting it to start at the beginning of the string again.
              • 4. Re: NoSuchElementException in StringTokenizer
                807597
                but the line has tokens....
                this is an example of the StringTokenizer in the
                array:

                trans 123 12-03-2004 credit 100.00

                i want to get the date out of the StringTokenizer
                hence i use the nextToken method.

                the error is thrown in the compare line...
                In general, when the JVM throws and exception which is defined as meaning there are no more tokens when you call nextToken() at a particular line, and you say but the line has tokens...

                I believe the JVM.

                Try this. Everywhere you want to call nextToken(), check hasMoreTokens() first.
                if (myStringTokenizer.hasMoreTokens())
                {
                   String nextToken = myStringTokenizer.nextToken();
                }
                else
                {
                    System.out.println( "Unexpectedly, I find I have no more tokens." );
                }
                good luck.

                RD-R
                � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                • 5. Re: NoSuchElementException in StringTokenizer
                  807597
                  sounds correct... but can you give me an example of my mistake?

                  supposedly:
                  StringTokenizer[] s = new StringTokenizer[3]
                  s[1] = trans 123 01-01-2001 credit 40.00
                  s[2] = trans 123 02-01-2001 debit 100.00
                  s[3] = trans 134 04-01-2001 credit 200.00

                  what will go wrong if i use the loop to compare?
                  • 6. Re: NoSuchElementException in StringTokenizer
                    807597
                    Out of curiosity, why are you sorting StringTokenizers?
                    • 7. Re: NoSuchElementException in StringTokenizer
                      796447
                      sounds correct... but can you give me an example of
                      my mistake?

                      supposedly:
                      StringTokenizer[] s = new StringTokenizer[3]
                      s[1] = trans 123 01-01-2001 credit 40.00
                      s[2] = trans 123 02-01-2001 debit 100.00
                      s[3] = trans 134 04-01-2001 credit 200.00

                      what will go wrong if i use the loop to compare?
                      Once you've called compare for a particular StringTokenizer instance, you've already advanced it to the end of the string.
                      So the next time you call compare, you run out of tokens.
                      Hint: Just store the strings, not StringTokenizers. In your compare method,
                      create and use a StringTokenizer in that method.
                      • 8. Re: NoSuchElementException in StringTokenizer
                        807597
                        i.e., parse that crap out, dump it into an object, store the object in a Collection, and use natural ordering or Comparator to sort...
                        • 9. Re: NoSuchElementException in StringTokenizer
                          807597
                          sounds correct... but can you give me an example of
                          my mistake?

                          supposedly:
                          StringTokenizer[] s = new StringTokenizer[3]
                          s[1] = trans 123 01-01-2001 credit 40.00
                          s[2] = trans 123 02-01-2001 debit 100.00
                          s[3] = trans 134 04-01-2001 credit 200.00

                          what will go wrong if i use the loop to compare?
                          When you compare s[1] to s[2], you consume both strings. The StringTokenizers now have no more tokens.

                          Then you compare s[2] to s[3]. Guess what? s[3] has tokens, but s[2] does not.

                          RD-R
                          � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                          • 10. Re: NoSuchElementException in StringTokenizer
                            796447
                            i.e., parse that crap out, dump it into an object,
                            store the object in a Collection, and use natural
                            ordering or Comparator to sort...
                            Yes, even better than repeatedly parsing each string over and over like you are now.
                            • 11. Re: NoSuchElementException in StringTokenizer
                              807597
                              When you compare s[1] to s[2], you consume both
                              strings. The StringTokenizers now have no more
                              tokens.
                              Or, more accurately, as pointed out by warnerja, the StringTokenizer has not consumed the String, but is now pointed to the end. No more tokens.

                              RD-R
                              � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                              • 12. Re: NoSuchElementException in StringTokenizer
                                807597
                                wow great thanks sharkura
                                • 13. Re: NoSuchElementException in StringTokenizer
                                  807597
                                  wow great thanks sharkura
                                  Actually, you should be listening to yawmark, and redesigning. He has a better idea (even though he likes Chevys).

                                  RD-R
                                  � {�                                                                                                                                                                                                                                                                                                                       
                                  • 14. Re: NoSuchElementException in StringTokenizer
                                    807597
                                    even though he likes Chevys...
                                    Only for patrol cars... ;o)
                                    1 2 Previous Next