• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot enter String even though prompted?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I wrote a short program to just get basic information anyways everything works(compiles) and does what i want it to do UNTIL it hits the name = input.nextLine(); part for some reason teh scanner will not let me enter the number it just skips it and prints out ""your name is" + name + "and the number you choose was" + number" so I don't even get a choice to choose the name in which I intended it to and coded it to.

any suggestions to why this is happening and how I can go about solving this issue thanks =)

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have the input before the prompt, but even if you switch those, you will run into a particular behavior of Scanner, which is that nextInt() does not consume the newline that you enter. You have to do something like this:

 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can surely find more info by typing "nextLine" in a search field > "Search this forum..." Beginning Java.
This question is something that comes up quite often and don't worry, you're not the one confused about it.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and this is where you start finding out about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!