Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By elbiociq
  • 1 Post By kneitzel

Thread: command to wait for user's input

  1. #1
    elbiociq is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default command to wait for user's input

    Hello,

    I am new to Java but not to programming, and I wonder what command there is available in Java to put a pause in the program sequence, for instance when you display "press any key to continue... "? I will appreciate any feedback. Thank you.
    Hieu App likes this.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: command to wait for user's input

    Hi,

    You can read the System.in stream directly or use the Scanner class to get user input.

    But Javas does not offer a getKey function, the Input of the user is available after the return key was pressed.

    So with a Scanner instance you could call nextLine() after asking the user to press the Enter key.

    Konrad
    elbiociq likes this.

  3. #3
    elbiociq is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: command to wait for user's input

    I implemented a solution using a string variable that accepts the user's input from the nextLine() method of the Scanner class, previously assigning an empty string "" to the string variable before the reading of the keyboard, then I compare the length of that variable and if the length is zero then the user pressed enter because the variable length remained the same before the reading. This condition evaluation is in a loop while evaluation until the value of the string variable is 0, and then it will exit the loop. Can you provide an example on how to use the System.in stream directly? Thank you.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: command to wait for user's input

    Hi,

    I would recommend to read
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    There is even a chapter about I/O from console.

Similar Threads

  1. Have java wait for user input
    By kkid in forum AWT / Swing
    Replies: 3
    Last Post: 02-14-2013, 06:02 PM
  2. Replies: 20
    Last Post: 02-06-2013, 08:27 PM
  3. User interaction command-line
    By kkid in forum New To Java
    Replies: 8
    Last Post: 10-29-2012, 01:37 PM
  4. Is there a way to wait for input from a button?
    By KingOfClubs in forum Networking
    Replies: 4
    Last Post: 04-21-2011, 09:14 PM
  5. Why doesnt my code wait for input?
    By Addez in forum New To Java
    Replies: 7
    Last Post: 08-19-2009, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •