This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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
 
Sheriff
Posts: 21453
96
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: 21453
96
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..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!