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.
java.util.Scanner
class (so that you do not need to write java.util.Scanner)
import java.util.Scanner ;
public class MyClassName {
...
There must be only one Scanner connected to the standard keyboard input stream.
private static final Scanner STDIN ;
new Scanner( System.in );
System.out.print( "Enter an int: " );
- hasNextDouble()
Attempt to read (but leave values in the buffer) a floating point decimal value of typedouble
. 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.
- hasNextInt()
Attempt to read (but leave values in the buffer) an integer value of typeint
. 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.
- 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.
- 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.
- nextDouble()
Attempt to read (and remove from buffer) a floating point decimal value of typedouble
. 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 validdouble
value.double floatingPointValue = STDIN.nextDouble();CAUTION: This leaves trailing whitespace including newline characters in the buffer.
- nextInt()
Attempt to read (and remove from buffer) anint
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 validint
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.
- 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.
- 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.
- 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.
- nextLine()
Read (and remove from buffer) all characters entered up to and including a new line character.String line = STDIN.nextLine();