This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

[Resolved]java.util.NoSuchElementException problem after displaying the information from text file

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all, I have taken this sample code and tried to edit to further understand about arraylist and reading information from text file. However, i am facing this problem after i have run the program.
run:
1073:Programming: 1
1057:Database: Concepts
1082:Computer: Organization
Exception in thread "main" java.util.NoSuchElementException
1074:Mathematics: for Computing
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at gradCheck.main(gradCheck.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

Here are the codes..
Class gradCheck


Class Course


Any comments on this topic would be great!
Thanks

Regards,
Jon
 
Sheriff
Posts: 22700
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Kho wrote:


After you check input.hasNext() once, you try to call input.next() and input.nextLine() no less than courseArray.length times. You should put that hasNext() check in the for-loop:
This still has a problem if a line does not contain both a course code and a course name though, but it's already more robust than your code.
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rob,
Thank you for replying on this topic.. I can roughly understand what you did to the current coding..



The function of .hasNext() is to Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.) aka the object has more than 1 attributes right? Anyway, I got the info from here.

Also i found another set of coding online just last night.. i am wondering which is more effective..

class ReadDVD

Class StringTest


regards,
Jon
 
Rob Spoor
Sheriff
Posts: 22700
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second one reads your entire file in memory, when only one line at a time is enough.


Jon Kho wrote:The function of .hasNext() is to Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.) aka the object has more than 1 attributes right?


1 or more.

Basically, for every call to next(), you should first check hasNext() to see if it would work. The same goes for Scanner's other methods: hasNextInt() should precede nextInt() etc. Only if you're certain there is more you can skip this hasNext() check (like your nextLine() call).
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically, for every call to next(), you should first check hasNext() to see if it would work. The same goes for Scanner's other methods: hasNextInt() should precede nextInt() etc. Only if you're certain there is more you can skip this hasNext() check (like your nextLine() call).



Thanks for the rule of thumb.. now i am trying to put the arraylist's values into the jTable.. it seems there is no luck and i might be posting it about it..
 
reply
    Bookmark Topic Watch Topic
  • New Topic