User input allows a Java program to receive information from the keyboard. Java provides classes such as Scanner and BufferedReader to read input entered by the user.
User input is information provided by a person while a program is running. For example, a program can ask for a student's name or age.
Example:
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
sc.close();
}
}
Example output:
Enter your name: Rahul
Hello, Rahul
The program reads the name entered by the user and displays a greeting.
Scanner is a Java class used to read
input from sources such as the keyboard, files,
and strings.
To use Scanner for keyboard input, import it:
import java.util.Scanner;
Create a Scanner object:
Scanner sc = new Scanner(System.in);
Scanner is the class name.sc is the variable holding the Scanner object.System.in represents standard input, usually the keyboard.
Use nextLine() to read a line of text,
including spaces.
import java.util.Scanner;
public class NameInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.println("Your name is " + name);
sc.close();
}
}
Example output:
Enter your full name: Rahul Kumar
Your name is Rahul Kumar
nextLine() reads the complete line
rather than stopping at the first space.
Use nextInt() to read an integer.
import java.util.Scanner;
public class AgeInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Your age is " + age);
sc.close();
}
}
Example output:
Enter your age: 20
Your age is 20
Enter a valid integer when using
nextInt(). Invalid input can cause
an InputMismatchException.
Use nextDouble() to read a decimal
number represented by the double data type.
import java.util.Scanner;
public class PriceInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter product price: ");
double price = sc.nextDouble();
System.out.println("Product price: " + price);
sc.close();
}
}
Example output:
Enter product price: 250.75
Product price: 250.75
The accepted decimal separator can depend on the Scanner's locale.
Scanner does not provide a dedicated
nextChar() method. You can read a
string and retrieve its first character.
import java.util.Scanner;
public class CharacterInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
String text = sc.nextLine();
if (!text.isEmpty()) {
char ch = text.charAt(0);
System.out.println("Character: " + ch);
} else {
System.out.println("No character entered.");
}
sc.close();
}
}
Example output:
Enter a character: A
Character: A
The method charAt(0) returns the
character at index zero. An empty string must
be checked before calling this method.
Use nextBoolean() to read a Boolean
value.
import java.util.Scanner;
public class BooleanInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Are you a student? ");
boolean isStudent = sc.nextBoolean();
System.out.println("Student: " + isStudent);
sc.close();
}
}
Example output:
Are you a student? true
Student: true
The input should be true or
false, ignoring case. Other text
can cause an InputMismatchException.
You can read different values using the same Scanner object.
import java.util.Scanner;
public class StudentInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter marks: ");
double marks = sc.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
sc.close();
}
}
Example output:
Enter name: Rahul Kumar
Enter age: 20
Enter marks: 85.5
Name: Rahul Kumar
Age: 20
Marks: 85.5
A common problem occurs when you call
nextLine() immediately after
nextInt(), nextDouble(),
or another token-reading method.
Problem example:
int age = sc.nextInt();
String name = sc.nextLine();
The numeric-reading method leaves the line separator
in the input. The next nextLine() may
consume the remaining part of that line immediately.
Correct approach:
System.out.print("Enter age: ");
int age = sc.nextInt();
sc.nextLine(); // Consume the remaining line
System.out.print("Enter full name: ");
String name = sc.nextLine();
The extra nextLine() consumes the
remaining line before the program asks for the name.
Scanner provides different methods for reading text.
| Method | What It Reads |
|---|---|
next() |
The next token, usually ending at whitespace. |
nextLine() |
The remaining text on the current line. |
import java.util.Scanner;
public class TextInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = sc.next();
sc.nextLine(); // Consume the rest of the line
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
System.out.println("Word: " + word);
System.out.println("Sentence: " + sentence);
sc.close();
}
}
Use next() for a token and
nextLine() for a full line of text.
BufferedReader is another way to read
text from the keyboard. It reads text as strings.
Numeric values must be parsed separately.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.print("Enter your age: ");
int age = Integer.parseInt(br.readLine());
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
readLine() returns a string, and
Integer.parseInt() converts valid
integer text into an integer.
In this example, throws IOException
declares that the method may propagate an input/output
exception.
When reading numeric input, users may enter an unexpected value. You can check the input before reading it.
import java.util.Scanner;
public class SafeInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
if (sc.hasNextInt()) {
int number = sc.nextInt();
System.out.println("Number: " + number);
} else {
System.out.println("Invalid integer input.");
}
sc.close();
}
}
Example output for invalid input:
Enter an integer: hello
Invalid integer input.
Methods such as hasNextInt() and
hasNextDouble() help check whether the
next token can be read as the expected numeric type.
Create a program that accepts a student's name, age, and marks and then displays the details.
import java.util.Scanner;
public class StudentDetails {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter marks: ");
double marks = sc.nextDouble();
System.out.println("\nStudent Details");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
sc.close();
}
}
Example output:
Enter student name: Rahul Kumar
Enter age: 20
Enter marks: 88.5
Student Details
Name: Rahul Kumar
Age: 20
Marks: 88.5
Key Points:
Scanner reads keyboard input.nextLine() reads a line of text.nextInt() reads an integer.nextDouble() reads a double value.nextBoolean() reads a Boolean value.BufferedReader can also read text input.1. Which Scanner method reads a complete line of text?
2. Which statement creates a Scanner for keyboard input?
3. Which method reads an integer from a Scanner?