• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stumped  RSS feed

 
J Sweeney
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to deal with this would be to consistently use keyboard.nextLine() for input.
 
James Harte
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's presume, for the moment that the data you are trying to process is as follows:

21.4
56.89
12

What this actually looks like is:
21.4\n56.89\n12\n

So, when you do nextDouble(), what is done is that the 21.4 is taken and what is left is:
\n56.89\n12\n

The next character is not a double and when a nextLine() is done, it sees an empty string.

This is the reason that after the nextDouble() you will want to consider having an extra nextLine().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!