• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

No such element error

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
kwek randy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to solve it. Thanks for the info.
 
reply
    Bookmark Topic Watch Topic
  • New Topic