In this tutorial, you'll learn how to read user input in Java using the Scanner class. This is a fundamental skill for creating interactive applications where you need to gather data from users. Understanding how to handle different types of input will make your programs more versatile and responsive.
User input is essential for any application that interacts with humans. In Java, the Scanner class provides a convenient way to read input from the user through the console. This tutorial will cover how to use the Scanner class to read various types of data, including strings, integers, and more.
The Scanner class is part of the java.util package and allows you to read different types of input from the user. It can be used to read from standard input (keyboard), files, or other input streams.
To use the Scanner class, you need to import it at the beginning of your Java program:
1
The nextLine() method reads a line of text from the user. It is useful for reading strings that may contain spaces.
1import java.util.Scanner;23public class ReadString {4public static void main(String[] args) {5Scanner scanner = new Scanner(System.in);67System.out.print("Enter your name: ");8String name = scanner.nextLine();910System.out.println("Hello, " + name + "!");1112scanner.close();13}14}
Enter your name: John Doe Hello, John Doe!
$ javac ReadString.java$ java ReadString
In this example, the program prompts the user to enter their name and then greets them with a personalized message.
The nextInt() method reads an integer value from the user. It is useful for reading numeric input that you want to perform calculations on.
1import java.util.Scanner;23public class ReadInteger {4public static void main(String[] args) {5Scanner scanner = new Scanner(System.in);67System.out.print("Enter your age: ");8int age = scanner.nextInt();910System.out.println("You are " + age + " years old.");1112scanner.close();13}14}
Enter your age: 30 You are 30 years old.
$ javac ReadInteger.java$ java ReadInteger
In this example, the program prompts the user to enter their age and then displays it back.
The Scanner class provides methods for reading other types of data as well, such as:
nextDouble() for double valuesnextFloat() for float valuesnextBoolean() for boolean values1import java.util.Scanner;23public class ReadMultipleTypes {4public static void main(String[] args) {5Scanner scanner = new Scanner(System.in);67System.out.print("Enter your name: ");8String name = scanner.nextLine();910System.out.print("Enter your age: ");11int age = scanner.nextInt();1213System.out.print("Enter your height in meters: ");14double height = scanner.nextDouble();1516System.out.println("Name: " + name);17System.out.println("Age: " + age);18System.out.println("Height: " + height + " meters");1920scanner.close();21}22}
Enter your name: Alice Enter your age: 25 Enter your height in meters: 1.65 Name: Alice Age: 25 Height: 1.65 meters
$ javac ReadMultipleTypes.java$ java ReadMultipleTypes
In this example, the program reads a string, an integer, and a double from the user and then displays them.
Scanner object using scanner.close() to free up system resources.1scanner.close();
InputMismatchException. You should handle such exceptions using try-catch blocks.1try {2int age = scanner.nextInt();3} catch (InputMismatchException e) {4System.out.println("Please enter a valid integer.");5}
Let's create a practical example that combines reading different types of input to calculate the BMI (Body Mass Index) of a user.
1import java.util.Scanner;23public class BMI {4public static void main(String[] args) {5Scanner scanner = new Scanner(System.in);67System.out.print("Enter your name: ");8String name = scanner.nextLine();910System.out.print("Enter your weight in kilograms: ");11double weight = scanner.nextDouble();1213System.out.print("Enter your height in meters: ");14double height = scanner.nextDouble();1516double bmi = weight / (height * height);1718System.out.println(name + ", your BMI is " + bmi);1920scanner.close();21}22}
Enter your name: Bob Enter your weight in kilograms: 70 Enter your height in meters: 1.75 Bob, your BMI is 22.857142857142858
$ javac BMI.java$ java BMI
In this example, the program reads a user's name, weight, and height, calculates their BMI, and then displays it.
Scanner class is used to read input from the user.nextLine() for reading strings.nextInt(), nextDouble(), etc., for reading different numeric types.Scanner object after use.In the next tutorial, we will explore how to work with dates in Java. Understanding date and time manipulation is crucial for many applications, such as scheduling systems or logging tools. Stay tuned!