wood burning stoves*
The moose likes Java in General and the fly likes No such element error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "No such element error" Watch "No such element error" New topic
Author

No such element error

kwek randy
Greenhorn

Joined: Jul 17, 2010
Posts: 2
Error: Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at LOTHA.main(LOTHA.java:49)
Java Result: 1



LOTHA.java file



Thank you


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

kwek randy wrote:Error: Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at LOTHA.main(LOTHA.java:49)
Java Result: 1


If you follow the link for the java.util.NoSuchElementException then you should see that it is:
"Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration."

What your error is says that the StringTokenizer.nextToken() method (called from your class at code line 49) tries to access the next token but that token does not exist. Why would this happen? You are trying to split your input into a certain number of parts, and the value actually stored in the input does not have that many parts. You have to figure out why the input doesn't have that many parts.


Steve
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
In other words, call StringTokenizer.hasMoreTokens() before calling StringTokenizer.nextToken() . You can also check whether line.length() is positive before calling new StringTokenizer(). Or you can get rid of the carriage return at the end of your input file :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38487
    
  23
And welcome to JavaRanch
kwek randy
Greenhorn

Joined: Jul 17, 2010
Posts: 2
I managed to solve it. Thanks for the info.
 
Don't get me started about those stupid light bulbs.
 
subject: No such element error