• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wait for user input

 
Greenhorn
Posts: 9
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not having indentation, just copied the code right off.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh great, thank you!
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic