To get user input in Java, we can use the Scanner class, which is part of the java.util package.
The Scanner class has many methods for reading input of various types.
Scanner ClassTo use the Scanner class, you must first import it and then create an object of the class.
import java.util.Scanner; // Import the Scanner classclass Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } }
Note: Interactive user input does not work in the simple code editor on this page. You would need to run this code in a local development environment or a more advanced online IDE to test it properly.
In the example above, we used the nextLine() method, which is used to read String values. To read other types, you can use the appropriate method:
| Method | Description |
|---|---|
nextBoolean() |
Reads a boolean value from the user |
nextByte() |
Reads a byte value from the user |
nextDouble() |
Reads a double value from the user |
nextFloat() |
Reads a float value from the user |
nextInt() |
Reads an int value from the user |
nextLine() |
Reads a String value from the user |
nextLong() |
Reads a long value from the user |
nextShort() |
Reads a short value from the user |
Scanner myObj = new Scanner(System.in);System.out.println("Enter name, age and salary:"); String name = myObj.nextLine(); // Reads string int age = myObj.nextInt(); // Reads integer double salary = myObj.nextDouble(); // Reads double
System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary);
nextLine() GotchaA very common issue beginners face when using Scanner is trying to read a String (using nextLine()) immediately after reading a numeric type (like nextInt() or nextDouble()).
When you enter a number and press Enter, nextInt() reads the number, but it leaves the "newline" character (created by pressing Enter) in the input buffer. The subsequent nextLine() command reads this leftover newline character and immediately finishes, seemingly "skipping" your chance to enter a string.
The Solution: Add an extra, empty nextLine() call to consume the leftover newline character before asking for the actual string.
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter your age:"); int age = scanner.nextInt(); // Consume the leftover newline scanner.nextLine(); System.out.println("Enter your full name:"); String name = scanner.nextLine(); // Now this works perfectly! System.out.println("Name: " + name + ", Age: " + age); } }
If you expect an integer (nextInt()) but the user types a word like "Hello", your program will crash with an InputMismatchException.
To prevent this, you should validate the input before reading it. The Scanner class provides methods like hasNextInt() and hasNextDouble() that check if the next piece of data is the correct type before attempting to read it.
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number:"); if (scanner.hasNextInt()) { int number = scanner.nextInt(); System.out.println("You entered a valid integer: " + number); } else { System.out.println("Error: That is not a valid integer!"); } } }
Which Scanner method is used to read a complete line of text (including spaces)?