Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner - exit while loop  RSS feed

 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I am trying t exit from a while loop but I got a nullPointerException when I enter any number/string.. Any idea ?

Thank you!!

 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your program is supposed to do, but you have set the variable c to null and that is why you are getting a null pointer exception.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanna format a number.

Ok, but if I swap with



I still have the same issue when I enter "ex"...
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line of code is throwing the exception? Hint: the stack trace which tells you what kind of exception happened also tells you what method the error was in and what line number.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I got...
Capture.PNG
[Thumbnail for Capture.PNG]
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you enter "ex", cs.hasNext() returns true, but c is still what it was after the previous iteration, so you enter the loop body. then you call cs.nextInt() which throws an exception because "ex" is not an integer.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah! SOlved!




Thank you chaps!!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Road wrote:Yeah! SOlved!

Well done!

Tip for the future: if you want to check a String that might be null against a literal, swap the test around, viz:
  while ( cs.hasNext() && !"ex".equals(c) ) { ...

I suspect that would also have solved your problem in this case.

Winston
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, using !"ex".equals(c) would not have solved the problem. The problem was not that c was null (it wasn't), but that the next token existed but was "ex", so trying to execute cs.nextInt() threw an exception.
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had there not been some sort of need to check that input equals "ex", it would simply have been possible to use
while (myScanner.hasNextInt()) ...
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. That would be a better way - wouldn't even need to type "ex" - ANY non-integer string would work to exit the loop
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!