• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mysterious NoSuchElementException

 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope someone can help with an error message I'm getting.
Why do I get an error when all I am doing is trying to read in a double from the keyboard using Scanner?
If I cut and paste the offending section of code into its own new java project it works fine.
The user has entered "1" as a menu choice, and so Main calls FindNearestAirportToMe.findNear().
I don't understand why it goes wrong at line 17. Any help much appreciated!


Here's the output:


Welcome to the Dabbo Air Navigation Project - version 1.0
GB only

What would you like to do ?
1. Find 5 nearest airports to Lat Long ?
2. Find 5 nearest airports by Post Code ?
3. Find 5 nearest navaids by Lat Long?
4. Find 5 nearest navaids by Post Code ?
5. Get distance and bearing between any two points ?
6. Get full details on any point ?
7. Select aircraft cruising speeds (in knots) ?
8. Generate PLOG ?
1
You chose Menu item 1
***About to call method
Enter your Latititude: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at FindNearestAirportToMe.findNear(FindNearestAirportToMe.java:17)
at Main.main(Main.java:16)



Here is class Main:


and FindNearestAirportToMe.findNear() :
 
Sergii Trotsenko
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you should add sc2.nextLine() to read new line after user's input before reading double.
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergii Trotsenko wrote:Hi,
I think you should add sc2.nextLine() to read new line after user's input before reading double.


I don't think so. If I cut and paste lines 15 to 20 into a new project it works fine.
Also my code copies exactly the example given in Oracle Java trails.
As far as I know there is no need to add the nextLine() method
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Dablin wrote:
Sergii Trotsenko wrote:Hi,
I think you should add sc2.nextLine() to read new line after user's input before reading double.


I don't think so. If I cut and paste lines 15 to 20 into a new project it works fine.
Also my code copies exactly the example given in Oracle Java trails.
As far as I know there is no need to add the nextLine() method



First of all ...



Don't close the scanner, if you are scanning from standard input. You really don't want to close standard input...

Second of all, did you happen to do that in your getMenuItem() method?

Henry
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a thought. I'll check it out tomorrow. I have multiple scanners concurrently. Maybe that's the problem.
I'll KUP. Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Dablin wrote:That's a thought. I'll check it out tomorrow. I have multiple scanners concurrently. Maybe that's the problem.
I'll KUP. Thanks!



KUP ???
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Dablin wrote:I have multiple scanners concurrently. Maybe that's the problem.


I haven't really been following this thread closely, so I don't know if that's the source of your problem, but it's definitely a bad idea to have multiple Scanners attached to the same input source.
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not yet solved the problem, but I think I am closer. If I comment out the call to getMenuItem() and manually make the choice 1, then the problem goes away in the other classes.
This tells me there is something wrong with my Menu class, but I can't figure out exactly what it is. I suspect it might be that I am not closing the stream properly, so I end up with
multiple Scanner(System.in)
My Menu class is:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . . KUP ???
I suspect it means “keep you posted”.
ED: Please read this; using abbreviations like that is an effective way to confuse people. Remember many of our readers did not learn English as a “first language”.
I think the best way to use Scanner from the keyboard is to use a utility class. If you search my posts for “utility class” and “Scanner”, both on this forum and this one, you should find examples which will guide you. That way you will not have two Scanners pointing to the same input source.
Henry has already warned you against closing the Scanner pointing to System.in, because you can never reopen System.in.
I do not know whether you are suffering from the nextLine pitfall, which is not well described in many books (some books even confuse you about nextLine). Look here.
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Henry Wong wrote: . . . KUP ???
I suspect it means “keep you posted”.
ED: Please read this; using abbreviations like that is an effective way to confuse people. Remember many of our readers did not learn English as a “first language”.
I think the best way to use Scanner from the keyboard is to use a utility class. If you search my posts for “utility class” and “Scanner”, both on this forum and this one, you should find examples which will guide you. That way you will not have two Scanners pointing to the same input source.
Henry has already warned you against closing the Scanner pointing to System.in, because you can never reopen System.in.
I do not know whether you are suffering from the nextLine pitfall, which is not well described in many books (some books even confuse you about nextLine). Look here.


Point taken & my apologies.
I appreciate the help you experts give beginners like me.
I will review those earlier postings. Best regards, Ed
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Dablin wrote:This tells me there is something wrong with my Menu class, but I can't figure out exactly what it is. I suspect it might be that I am not closing the stream properly, so I end up with
multiple Scanner(System.in)


One option is to *not* create multiple scanners on standard input -- have only one, that is maintained by one of the classes, and is called from by the other classes.

In the meantime, if you don't want to do such a refactoring effort, just try *not* closing the scanner. Just let the scanner object go out of scope -- and see if that works.

Henry
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm now satisfied my problem was due to multiple scanners. It turns out that, while there is no problem having multiple scanners in the code for different purposes, you cannot reopen a closed Scanner(System.in)
Marking this as resolved. Thanks all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic