Java User Input

Java User Input (Scanner)

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.


Using the Scanner Class

To use the Scanner class, you must first import it and then create an object of the class.

Reading a Line of Text

import java.util.Scanner;  // Import the Scanner class

class 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.


Input Types

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

Reading Different Data Types

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);


Advanced: The nextLine() Gotcha

A 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.

Fixing the `nextLine()` Gotcha

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); } }


Advanced: Input Validation

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.

Validating Input

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!"); } } }


Exercise

?

Which Scanner method is used to read a complete line of text (including spaces)?