Input in Java Programming
In this lesson, we will understand what is Scanner class, and how it is used in the Java program to accept input with the help of some examples.
Input in Java
The Scanner class is used in Java programs to accept input from the standard input device (keyboard) and store it in one or more variables. The Scanner class is available in the java.util package.
To use the Scanner class, we need to create an object of the Scanner class and use the available methods in the class to take different types of input from the user. The available methods in the Scanner class are listed below.
| Method | Description |
| nextBoolean() | Reads a boolean type value from the user |
| nextByte() | Reads a byte type value from the user |
| nextDouble() | Reads a double type value from the user |
| nextFloat() | Reads a float type value from the user |
| nextInt() | Reads an int type value from the user |
| nextLong() | Reads a long type value from the user |
| nextShort() | Reads a short type value from the user |
| nextLine() | Reads a String type value from the user |
Let's see an example of how to use Scanner class to read different types of data from the keyboard.
import java.util.Scanner; // Import the Scanner class
public class Example
{
public static void main(String[] args)
{
int age;
float height;
double weight;
char initial;
// Create a Scanner object to read input
Scanner sc = new Scanner(System.in);
// Ask the user to enter their age
System.out.print("Enter your age: ");
age = sc.nextInt();
// Ask the user to enter their height
System.out.print("Enter your height in meters: ");
height = sc.nextFloat();
// Ask the user to enter their weight
System.out.print("Enter your weight in kilogram: ");
weight = sc.nextDouble();
// Consume the newline character left by the sc.nextDouble()
sc.nextLine(); // This fixes the issue
// Ask the user to enter the first character of their name
System.out.print("Enter the first character of your name: ");
initial = sc.nextLine().charAt(0); // Reads the first character from input
// Print the values entered by the user
System.out.println("\nYou entered:");
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Weight: " + weight);
System.out.println("Initial: " + initial);
}
}
Enter your age: 15 Enter your height in meters: 3.423 Enter your weight in kilogram: 45.21543 Enter the first character of your name: R You entered: Age: 15 Height: 3.423 Weight: 45.21543 Initial: R
- 1. Prompting the User:
- System.out.print is used to ask the user to enter their age, height, weight, and initial.
- 2. Reading Input:
- age = sc.nextInt(); reads an integer type number input by the user and stores it in the variable age.
- height = sc.nextFloat(); reads a float type number input by the user and stores it in the variable height.
- weight = sc.nextDouble(); reads a double type number input by the user and stores it in the variable weight.
- sc.nextLine() consume the newline character left by the sc.nextDouble().
- initial = sc.nextLine().charAt(0); reads the first character from the user input and stores it in the variable initial.
- 3. Displaying the Input:
- System.out.println() shows the values entered by the user, confirming they were read correctly.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.