Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Loop causing NoSuchElementException

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm running into one issue with this code that doesn't make sense. The code works without the do while loop shown in the first code block. When I add that or a while loop etc, then it breaks.
It will print the menu, ask "Please enter your selection" I can input number 1 and the JFileChooser pops up. Then when I choose the text file it breaks.

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.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at addressBook.ContactDriver.menuDiplay(ContactDriver.java:113)
at addressBook.ContactDriver.main(ContactDriver.java:56)



Line 113: menuSelect = kb.nextInt ( );    (seen in code block #2)
Line 56: menuSelection = menuDiplay ( );   (seen in code block #1)


Code block #1




Code block #2




Code block #3




It works fine without the loop, but I don't understand why the loop is causing this to break. The intention is to have the menu repeat until option 10 is chosen which terminates the program.
 
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NEVER close a Scanner created with System.in
 
Nick Smithson
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:NEVER close a Scanner created with System.in


Eclipse and my prof both say that I have to though. =/ and even when I tried to comment that out I still have the same issue with the code crashing in that loop.
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you removed all the kb.close() including the line after menuSelect = kb.nextInt ( ); ?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and also the line after while(menuSelection != 10); ?
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to consume a token that is not there. you do a number of next() calls without checking if there is next.

one thing you can use is:


or

After that you can safely call
next() or nextInt() methods.
 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikki Smith wrote:. . . Eclipse and my prof both say that I have to though. . . .

In which case both are mistaken.

Carey is correct. You need to learn which of Eclipse's warnings to believe and which to ignore and which to squelch. You can shut Eclipse up with the appropriate annotation:-
@SuppressWarnings("resource")
Check the spelling carefully . . . or use Eclipse's suggestion to add that annotation and it will put the correct spelling in the correct place. Click on the yellow mark and you get a dropdown list of possible corrections. You need to learn which of Eclipse's suggested corrections to believe: in this case adding the annotation.
I am going to give the opposite advice: don't create Scanner objects pointing to System.in. I know you won't believe me, but if you read it carefully you will see I said objects. You are allowed to create one Scanner object and you shou‍ld use that for all your keyboard input (as far as possible). The ideal would be for it to be in a utility class of its own. Never close that Scanner because if you do you close System.in and you can never reopen it. That is why you are getting the no such element exceptions. Similarly you can close System.in by feeding it ctrl‑D or ctrl‑Z but both those keystrokes cause the same damage. There are other ways you can close System.in, for example what the OP in this thread did. All of them will produce the same disastrous result.

I know you have been told to close Scanners but there is one exception to that rule: Scanners pointing to System.in. Always close Scanners pointing to everything else; this is best done with try with resources.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic