User Input from the keyboard via a java.util.Scanner object

Use a java.util.Scanner object to read user input from the keyboard while your program is running.  The user input can be imagined as characters on a stream. When the user types the [Enter] key, a newline character, \n , is put on the stream, the input is stored in a buffer and thus becomes available to the program. More typing can be pictured as more letters on the next line in the buffer.  The letters stay in the buffer until they are "read" by one of the methods of the Scanner class.

input stream contains "123 abc 12\nJava is fun!\n

0. import the java.util.Scanner class (so that you do not need to write java.util.Scanner)

import java.util.Scanner ;
public class MyClassName {
   ...

1. Declare a single static class constant (in a single class outside of any method)

There must be only one Scanner connected to the standard keyboard input stream.

private static final Scanner STDIN ;

2. Create the instance using System.in (keyboard input) and assign it to the Scanner constant you created.

new Scanner( System.in );

3. Prompt the user for desired input. (This is part of method block)

System.out.print( "Enter an int: " );

4. There are a few methods options for determining if the next available user input waiting in the Scanner buffer is a particular data type.

  1. hasNextDouble()
    Attempt to read (but leave values in the buffer) a floating point decimal value of type double.  This method will skip whitespace including newlines to get to digits, and it will return false if the next non-whitespace characters can not be converted to a valid floating point decimal value. 

    if ( STDIN.hasNextDouble() )

    CAUTION: This method does not return any user input, just true or false as indicated.  All user input is left in the buffer.

  2. hasNextInt()
    Attempt to read (but leave values in the buffer) an integer value of type int.  This method will skip whitespace including newlines to get to digits, and it will return false if the next non-whitespace characters can not be converted to a valid int value. 

    if ( STDIN.hasNextInt() )

    CAUTION: This method does not return any user input, just true or false as indicated.  All user input is left in the buffer.

  3. hasNext()
    Attempt to read (but leave values in the buffer) an string of characters (word).  This method will skip whitespace including newlines to get to characters, and it will return false only if the next non-whitespace characters can not be converted to a valid String value. 

    if ( STDIN.hasNext() )

    CAUTION: This method does not return any user input, just true or false as indicated.  All user input is left in the buffer.

  4. hasNextLine()
    Attempt to read (but leave values in the buffer) a string of characters up to the newline character.  This method returns true if and only if there is a new line available to read. 

    if ( STDIN.hasNextLine() )

    CAUTION: This method does not return any user input, just true or false as indicated.  All user input is left in the buffer.

5. There are several method options for reading (consuming) input from the Scanner buffer.

  1. nextDouble()
    Attempt to read (and remove from buffer) a floating point decimal value of type double.  This method will skip whitespace including newlines to get to digits, but it will throw an exception if the next non-whitespace characters can not be converted to a valid floating point decimal value.  TypeMismatchException is thrown if the next non-whitespace characters can not be converted to a valid double value.

    double floatingPointValue = STDIN.nextDouble();

    CAUTION: This leaves trailing whitespace including newline characters in the buffer.

  2. nextInt()
    Attempt to read (and remove from buffer) an int value.  This method will skip whitespace including newlines to get to digits, but it will throw an exception if the next non-whitespace characters can not be converted to a valid integer.  TypeMismatchException is thrown if the next non-whitespace characters can not be converted to a valid int value.    When connected to the keyboard via the standard input stream (System.in), nextInt() will wait for the user to type some input and then press [Enter], if the buffer is empty.

    int intValue = STDIN.nextInt();

    CAUTION: This leaves trailing whitespace including newline characters in the buffer.

  3. next()
    Attempt to read (and remove from buffer) a word or string of characters.  This method will skip whitespace including newlines to get to letters and digits and will return a sequence of all characters upto but not including a space or newline character.  When connected to the keyboard via the standard input stream (System.in), next() will wait for the user to type input and then press [Enter], if the buffer is empty.

    String word = STDIN.next();
    

    CAUTION: This leaves trailing whitespace including newline characters in the buffer.

  4. hasNextInt()
    Determine if there is an int value waiting in the input stream buffer.  This method will skip whitespace including newlines to get to letters and digits and will return a true if the next sequence of characters upto but not including a space or newline character can be converted to an int value.

    boolean hasInt = STDIN.hasNextInt()

    CAUTION: This does not get the int value from the buffer.  It just lets you know if the next non-whitespace characters can be converted to an int or not.

  5. hasNextDouble()
    Determine if there is an double value waiting in the input stream buffer.  This method will skip whitespace including newlines to get to letters and digits and will return a true if the next sequence of characters upto but not including a space or newline character can be converted to a double value.

    boolean hasDouble = STDIN.hasNextDouble()

    CAUTION: This does not get the double value from the buffer.  It just lets you know if the next non-whitespace characters can be converted to a double or not.

  6. nextLine()
    Read (and remove from buffer) all characters entered up to and including a new line character.

    String line = STDIN.nextLine(); 
© 2014-2020 Debra Deppeler, All rights reserved.