⏱️ 9 min read • Beginner Level • Lesson 19
Completed on . You can revise this lesson or continue to the next topic.
In Java, input is commonly taken using the Scanner class,
and output is displayed using methods like print(),
println(), and printf().
Before learning input and output, you should understand Java variables, Java data types, and Java literals.
The Scanner class in Java is used to read input from different sources,
such as keyboard input, files, and strings. For beginner programs, it is commonly used
to take input from the keyboard using System.in.
Scanner belongs to the java.util package.
It breaks input into tokens using whitespace as the default delimiter.
It provides methods to read values such as int,
double, String, and boolean.
| Method | Description |
|---|---|
next() |
Reads a single word as a String. |
nextLine() |
Reads a complete line including spaces. |
nextByte() |
Reads a byte value. |
nextShort() |
Reads a short value. |
nextInt() |
Reads an int value. |
nextLong() |
Reads a long value. |
nextFloat() |
Reads a float value. |
nextDouble() |
Reads a double value. |
nextBoolean() |
Reads a boolean value. |
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String name = sc.next();
sc.close();
import java.util.Scanner;
The following program reads a name, ID, and salary from the user using the
Scanner class.
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = sc.next();
System.out.print("Enter ID: ");
int id = sc.nextInt();
System.out.print("Enter Salary: ");
double salary = sc.nextDouble();
System.out.println("----------");
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
sc.close();
}
}
Sample Input:
Ayan 123 20000
Output:
Enter Name: Enter ID: Enter Salary: ---------- ID: 123 Name: Ayan Salary: 20000.0
next() for a single word and
nextLine() for a full line including spaces.
A common beginner mistake happens when nextLine() is used after
methods like nextInt(), nextDouble(), or nextFloat().
These numeric methods leave the newline character in the input buffer, and the next
nextLine() reads that leftover newline.
import java.util.Scanner;
public class ScannerPitfall {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter full name: ");
String fullName = sc.nextLine(); // reads leftover newline
System.out.println("Age: " + age);
System.out.println("Name: " + fullName);
sc.close();
}
}
Output:
Enter age: 25 Enter full name: Age: 25 Name:
To fix this, call an extra nextLine() after numeric input to consume
the leftover newline.
import java.util.Scanner;
public class ScannerFixed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
sc.nextLine(); // consume leftover newline
System.out.print("Enter full name: ");
String fullName = sc.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + fullName);
sc.close();
}
}
Output:
Enter age: 25 Enter full name: Alice Johnson Age: 25 Name: Alice Johnson
| Method | Reads | Spaces Allowed? |
|---|---|---|
| next() | Single word | No |
| nextLine() | Entire line | Yes |
Java provides different methods to display output on the console.
The most common output methods are print(),
println(), and printf().
| Method | Use |
|---|---|
System.out.print() |
Prints output on the same line. |
System.out.println() |
Prints output and then moves to the next line. |
System.out.printf() |
Prints formatted output using format specifiers. |
print() does not move the cursor to a new line after printing,
while println() moves the cursor to the next line.
public class PrintVsPrintln {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.println("world!");
System.out.println("Bye");
}
}
Output:
Hello, world! Bye
The printf() method is used for formatted printing.
It works similarly to C language printf and supports
format specifiers like %d, %f, %s, and %n.
System.out.printf(String format, Object... args);
| Specifier | Meaning |
|---|---|
%d |
Integer value |
%f |
Floating-point value |
%s |
String value |
%c |
Character value |
%b |
Boolean value |
%n |
Platform-independent new line |
%% |
Prints a percent sign |
public class PrintfExample {
public static void main(String[] args) {
int id = 42;
double price = 123.456;
String name = "Alice";
System.out.printf("ID: %05d%n", id);
System.out.printf("Price: %.2f%n", price);
System.out.printf("Name: %-10s End%n", name);
}
}
Output:
ID: 00042 Price: 123.46 Name: Alice End
printf(), use %n instead of
\n for a platform-independent new line.
Scanner class is used to read input in Java.next() reads one word, while nextLine() reads a full line.nextLine() before reading a full line.print() prints on the same line.println() prints output and moves to the next line.printf() is used for formatted output.🧠 Test your understanding with a quick quiz
Topic: Input-output | Language: Java
Scanner sc = new Scanner(System.in);java.util.Date now = new java.util.Date();
System.out.printf("ISO date: %___%n", now);int x = 5;
System.out.____("Value: %d%n", x);int age = sc.nextInt();
// fix here
String name = sc.nextLine();Scanner sc = new Scanner(System.in);
String line = sc._____();🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.