• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wait for user input  RSS feed

 
Johan Lorentzon
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm very very new in JAVA so I apologize for stupid questions, but anyway- here I go.

I'm doing very simple exercises at this point as I just started programming again after 8 years, and I can't get the console to wait for user input. At this point I can only get one user input and use it. My code is as follow below, don't know how to get the program to wait for the next input before executing the rest of the code.

Thank you in advance!



 
Johan Lorentzon
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not having indentation, just copied the code right off.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the code tags for you. Doesn't it look better? UseCodeTags <-- that's a link explaining how to use them
 
Johan Lorentzon
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh great, thank you!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us what your Java® book says Scanner#nextLine does. The problem is described in this post, along with a possible solution. Call nextLine twice: change line 27 to read
input.nextLine();
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw, Campbell will be along soon to explain why scanner doesn't work the way you think it does. In the meantime, this thread might help.

[edit] - damn. beat me by 15 seconds
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:btw, Campbell will be along soon . . .
What a stroke of clairvoyance.
 
Johan Lorentzon
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, feels good to be on the here

I altered my code as follow bellow, it fixed the problem but I´m not really sure if that's the best way of doing it. Anyway, I'll keep looking in the post you guys gave me and see if I'll find something more. This solution just came to so I didn't find it and therefore not certain it's the best Cheers for your posts!

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johan Lorentzon wrote:Thank you, feels good to be on the here

I altered my code as follow bellow, it fixed the problem but I´m not really sure if that's the best way of doing it. . . .
I am afraid it isn't. There is no point in having multiple Scanners for System.in. It is much quicker simply to call nextLine twice. Did you read the old post which JKR posted?
 
Johan Lorentzon
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, think I got it now! (At least I hope so because it works fine now without an extra scanner )



Btw, great forum so far, thanks for helping!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!