Java Input and Output – Complete Guide Using Scanner, BufferedReader, and System.out


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

  1. Output refers to displaying data to the console.
  2. Java uses the System.out class for output.

1.1 Using System.out.println()

  1. 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()

  1. Prints message without newline

System.out.print("Hello ");
System.out.print("World");

Output:


Hello World

1.3 Using System.out.printf()

  1. Prints formatted output (like C printf)
  2. 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

  1. Input refers to reading data from user
  2. Common methods: Scanner and BufferedReader

2.1 Using Scanner Class

  1. Part of java.util package
  2. 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

  1. Part of java.io package
  2. More efficient for reading large input
  3. 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
Packagejava.utiljava.io
SpeedSlowerFaster
Exception HandlingNo checked exceptionsRequires try-catch or throws IOException
ParsingCan 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

  1. Output: System.out.println(), System.out.print(), System.out.printf()
  2. Input: Scanner (easy) and BufferedReader (efficient)
  3. Input/output is essential for interactive programs
  4. Scanner handles primitive types and strings, BufferedReader requires parsing and exception handling