GETTING INPUT FROM THE
KEYBOARD
1. Scanners
2. BufferedReader
3. JOption Pane
Using Scanners
Interactive programs
• We have written programs that print console output, but it is also
possible to read input from the console.
• The user types input into the console. We capture the input and use it in our
program.
• Such a program is called an interactive program.
• Interactive programs can be challenging.
• Computers and users think in very different ways.
• Users misbehave.
Input and [Link]
• [Link]
• An object with methods named println and print
• [Link]
• not intended to be used directly
• We use a second object, from a class Scanner, to help us.
• Constructing a Scanner object to read console input:
Scanner name = new Scanner([Link]);
• Example:
Scanner console = new Scanner([Link]);
Java class libraries, import
• Java class libraries: Classes included with Java's JDK.
• organized into groups named packages
• To use a package, put an import declaration in your program.
• Syntax:
// put this at the very top of your program
import packageName.*;
• Scanner is in a package named [Link]
import [Link].*;
• To use Scanner, you must place the above line at the top of your program (before
the public class header).
Scanner methods
Method Description
nextInt() reads a token of user input as an int
nextDouble() reads a token of user input as a double
next() reads a token of user input as a String
nextLine() reads a line of user input as a String
• Each method waits until the user presses Enter.
• The value typed is returned.
[Link]("How old are you? "); // prompt
int age = [Link]();
[Link]("You'll be 40 in " +
(40 - age) + " years.");
• prompt: A message telling the user what input to type.
Example Scanner usage
import [Link].*; // so that I can use Scanner
public class ReadSomeInput {
public static void main(String[] args) {
Scanner console = new Scanner([Link]);
[Link]("How old are you? ");
int age = [Link]();
[Link](age + "... That's quite old!");
}
}
• Output (user input underlined):
How old are you? 14
14... That's quite old!
Another Scanner example
import [Link].*; // so that I can use Scanner
public class ScannerSum {
public static void main(String[] args) {
Scanner console = new Scanner([Link]);
[Link]("Please type three numbers: ");
int num1 = [Link]();
int num2 = [Link]();
int num3 = [Link]();
int sum = num1 + num2 + num3;
[Link]("The sum is " + sum);
}
}
• Output (user input underlined):
Please type three numbers: 8 6 13
The sum is 27
• The Scanner can read multiple values from one line.
Input tokens
• token: A unit of user input, as read by the Scanner.
• Tokens are separated by whitespace (spaces, tabs, newlines).
• How many tokens appear on the following line of input?
23 John Smith 42.0 "Hello world" $2.50 " 19"
• When a token is not the type you ask for, it crashes.
[Link]("What is your age? ");
int age = [Link]();
Output:
What is your age? Timmy
[Link]
at [Link](Unknown Source)
at [Link](Unknown Source)
...
Using BufferedReaders
Using JOptionPane
Exercises 1 (Individual) : Submit to EDMODO
Scanner BufferedReader JOptionPane
Input 3 words:
Hello
First Name
Last Name
1 2 3
Printing all three words:
Hello First Name Last Name
Input 5 random integers:
6
4
10
4 5 6
20
1
Printing the sum of the
integers:
31
Number 1: Using Scanner
CODE SAMPLE OUTPUT