Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
1057atabase: 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
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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..
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!