Learn how to take input from the user and display output in Java using Scanner, BufferedReader, and System.out.println() with complete examples for beginners.
Input and Output in Java – Complete Detailed Tutorial
Input and output are essential for any Java program. Java provides multiple ways to read data from the user and display results.
1. Output in Java
- Output refers to displaying data to the console.
- Java uses the
System.outclass for output.
1.1 Using System.out.println()
- Prints message with a newline at the end
System.out.println("Hello, Java!");
Example:
int sum = 50;
System.out.println("The sum is: " + sum);
Output:
The sum is: 50
1.2 Using System.out.print()
- Prints message without newline
System.out.print("Hello ");
System.out.print("World");
Output:
Hello World
1.3 Using System.out.printf()
- Prints formatted output (like C
printf) - Syntax:
System.out.printf("Name: %s, Age: %d", name, age);
Example:
String name = "John";
int age = 25;
System.out.printf("Name: %s, Age: %d", name, age);
Output:
Name: John, Age: 25
2. Input in Java
- Input refers to reading data from user
- Common methods:
ScannerandBufferedReader
2.1 Using Scanner Class
- Part of
java.utilpackage - Easy to use for reading primitive data types and strings
Syntax:
Scanner sc = new Scanner(System.in);
Example – Read integer and string:
import java.util.Scanner;
public class InputDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", Age: " + age);
}
}
Sample Output:
Enter your name: John
Enter your age: 25
Hello John, Age: 25
2.2 Using BufferedReader Class
- Part of
java.iopackage - More efficient for reading large input
- Needs exception handling
Syntax:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Example – Read string and integer:
import java.io.*;
public class BufferedReaderDemo {
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("Hello " + name + ", Age: " + age);
}
}
3. Differences Between Scanner and BufferedReader
| FeatureScannerBufferedReader | ||
| Package | java.util | java.io |
| Speed | Slower | Faster |
| Exception Handling | No checked exceptions | Requires try-catch or throws IOException |
| Parsing | Can directly parse int, float, etc. | Needs Integer.parseInt() or Double.parseDouble() |
4. Reading Different Data Types with Scanner
| MethodDescription | |
| nextInt() | Read integer |
| nextFloat() | Read float |
| nextDouble() | Read double |
| next() | Read single word (string) |
| nextLine() | Read entire line (string) |
| nextBoolean() | Read boolean |
Example:
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.print("Enter text: ");
String text = sc.next();
5. Example Program – Input and Output
import java.util.Scanner;
public class IODemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
int diff = a - b;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + diff);
}
}
Sample Output:
Enter first number: 15
Enter second number: 5
Sum: 20
Difference: 10
6. Summary
- Output:
System.out.println(),System.out.print(),System.out.printf() - Input:
Scanner(easy) andBufferedReader(efficient) - Input/output is essential for interactive programs
- Scanner handles primitive types and strings, BufferedReader requires parsing and exception handling