aspose file tools*
The moose likes Beginning Java and the fly likes Mysterious NoSuchElementException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mysterious NoSuchElementException" Watch "Mysterious NoSuchElementException" New topic
Author

Mysterious NoSuchElementException

Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
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

Joined: Oct 23, 2012
Posts: 6
Hi,
I think you should add sc2.nextLine() to read new line after user's input before reading double.
Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 09, 2012
Posts: 32
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Oct 09, 2012
Posts: 32
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
Apology accepted
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Oct 09, 2012
Posts: 32
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mysterious NoSuchElementException