User Input in Java
Today, we'll learn how to get user input in Java, which is essential for creating interactive programs.
Getting User Input in Java
To get user input in Java, we commonly use the Scanner class from the java.util package. The Scanner class allows us to read different types of data from various input sources, such as the keyboard.
Steps to Get User Input
- Import the Scanner Class
- Create a Scanner Object
- Read User Input
1. Import the Scanner Class
First, we need to import the Scanner class at the beginning of our program.
import java.util.Scanner;
2. Create a Scanner Object
Next, we create a Scanner object. This object will allow us to read input from the standard input stream (keyboard).
Scanner scanner = new Scanner(System.in);
3. Read User Input
The Scanner class provides various methods to read different types of input:
- nextLine(): Reads a line of text.
- nextInt(): Reads an integer.
- nextDouble(): Reads a double.
- nextBoolean(): Reads a boolean.
Examples
Some examples are given below
1. Reading a 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 name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Explanation:
We import the Scanner class.
We create a Scanner object named scanner.
We prompt the user to enter their name.
We read the user's input using scanner.nextLine().
We display a greeting message with the user's name.
2. Reading an Integer
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();
System.out.println("You are " + age + " years old.");
}
}
Explanation:
We prompt the user to enter their age.
We read the user's input using scanner.nextInt().
We display the user's age.
3. Reading a Double
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your height in meters:");
double height = scanner.nextDouble();
System.out.println("Your height is " + height + " meters.");
}
}
Explanation:
We prompt the user to enter their height in meters.
We read the user's input using scanner.nextDouble().
We display the user's height.
4. Reading a Boolean
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Are you a student? (true/false):");
boolean isStudent = scanner.nextBoolean();
if (isStudent) {
System.out.println("You are a student.");
} else {
System.out.println("You are not a student.");
}
}
}
Explanation:
We prompt the user to indicate if they are a student.
We read the user's input using scanner.nextBoolean().
We display a message based on whether the user is a student.
Closing the Scanner
After you are done using the Scanner, it's good practice to close it to free up resources.
scanner.close();
Summary
In this tutorial, we learned how to:
Import the Scanner class.
Create a Scanner object.
Read different types of user input: String, int, double, and boolean.
Close the Scanner to free up resources.
Using the Scanner class is a simple and effective way to get user input in Java.