1
- Scanner
Readings: 3.4
- We have written programs that print console output.
It is also possible to read input from the console.
The user types the input into the console. The program uses the input to do something. Such a program is called an interactive program.
- Interactive programs can be challenging.
Computers and users think in very different ways. Users tend to “misbehave”.
- System.in
System.out is an object!
It has the methods named println and print for
printing to the console.
We read input using an object named System.in
System.in is not intended to be used directly. We will use another object, from a class called Scanner,
to read input from System.in.
- Scanner
Constructing a Scanner object to read the console:
Scanner <name> = new Scanner(System.in);
Example:
Scanner console = new Scanner(System.in);
- Scanner
Some methods of Scanner: Each of these methods pauses your program until
the user types input and presses Enter.
Then the value typed is returned to your program.
reads and returns user input as a double nextDouble() reads and returns user input as an int nextInt() reads and returns user input as a String next() Description Method