Java User Input (Scanner)
So far every program printed the same thing every time. Programs become interactive when they ask the user for input. In Java the easiest way to read what the user types is the Scanner class. In our editor, when a program waits for input, you type in the terminal and press Enter.
Reading a line of text
Scanner lives in the java.util package, so the program starts with an import line. Then you create a scanner that reads from System.in, the keyboard:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("What is your name? ");
String name = in.nextLine();
System.out.println("Hello, " + name + "!");
}
}Priya
What is your name? Hello, Priya!
nextLine() waits until the user presses Enter and returns everything they typed on that line. The prompt uses print, not println, so the answer is typed on the same line. (In the output below the example, what you type is not repeated, which is why "Hello" follows the question directly.)
Reading numbers
nextInt() reads a whole number and nextDouble() a decimal number. They return real numbers, ready for calculations:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Price of one item: ");
double price = in.nextDouble();
System.out.print("How many? ");
int count = in.nextInt();
System.out.printf("Total: %.2f%n", price * count);
}
}12.5 4
Price of one item: How many? Total: 50.00
If the user types something that is not a number, nextInt throws an InputMismatchException. In the Exceptions topic you will learn to catch it and ask again.
The leftover new line
There is one famous trap. nextInt() reads the number but leaves the Enter key in the input. A nextLine() right after it then reads that empty rest of the line. The fix is to call nextLine() once more to skip it:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Age: ");
int age = in.nextInt();
in.nextLine(); // skip the rest of the line after the number
System.out.print("City: ");
String city = in.nextLine();
System.out.println(age + " years old, from " + city + ".");
}
}24 New Delhi
Age: City: 24 years old, from New Delhi.
Another common solution is to read every line with nextLine() and convert it with Integer.parseInt, as you saw in Type Casting.
Several values on one line
next() reads one word, and nextInt() can read several numbers from the same line, separated by spaces:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Three numbers: ");
int a = in.nextInt(), b = in.nextInt(), c = in.nextInt();
System.out.println("Sum: " + (a + b + c));
}
}4 9 12
Three numbers: Sum: 25
Practice exercises
- Ask for the user's name and favourite colour and print a sentence with both.
- Ask for the length and width of a room and print its area.
- Ask for a temperature in Celsius and print it in Fahrenheit (
c * 9 / 5 + 32).