Input and Output (I/O) operations are among the most essential concepts in Java programming. Almost every Java application interacts with users by accepting input and displaying output. Whether you’re developing a console application, desktop software, Android app, or enterprise system, understanding Java input and output is a fundamental skill.
Java provides several ways to accept input from users. The most commonly used approach is the Scanner class, which is simple and beginner-friendly. Java also supports BufferedReader, which is faster and often preferred in competitive programming and performance-critical applications.
For displaying output, Java provides methods such as System.out.print(), System.out.println(), and System.out.printf() for formatted printing.
In this chapter, you’ll practice Java programs that cover different input and output techniques, helping you build confidence in reading user input, displaying results, and formatting output.
You’ll learn concepts including:
- Scanner Class
- BufferedReader
- Reading Integer Input
- Reading Floating-Point Numbers
- Reading Strings
- Reading Characters
- Multiple User Inputs
- Formatted Output using
printf()
Each practice question includes a complete Java solution, sample input, sample output, explanation, and concepts covered, making it ideal for beginners, college students, placement preparation, and technical interviews. Java Input and Output practice questions with solutions help to understand the concepts.
1. Java Program to Read an Integer Using Scanner Class
Problem Statement
Write a Java program to accept an integer from the user using the Scanner class and display it.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter an Integer: ");
number = scanner.nextInt();
System.out.println("You Entered: " + number);
scanner.close();
}
}
Sample Input
Enter an Integer: 50
Sample Output
You Entered: 50
Explanation
The Scanner class reads an integer from the keyboard using the nextInt() method and stores it in an integer variable.
Concepts Covered
- Scanner Class
- Integer Input
- Variables
- User Input
2. Java Program to Read a Floating-Point Number
Problem Statement
Write a Java program to accept a decimal number from the user and display it.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double number;
System.out.print("Enter a Decimal Number: ");
number = scanner.nextDouble();
System.out.println("You Entered: " + number);
scanner.close();
}
}
Sample Input
Enter a Decimal Number: 45.75
Sample Output
You Entered: 45.75
Explanation
The nextDouble() method reads a floating-point value entered by the user.
Concepts Covered
- Scanner Class
- double Data Type
- User Input
3. Java Program to Read a String Using Scanner
Problem Statement
Write a Java program to accept a single-word string from the user.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
System.out.print("Enter Your Name: ");
name = scanner.next();
System.out.println("Welcome " + name);
scanner.close();
}
}
Sample Input
Enter Your Name: Rishabh
Sample Output
Welcome Rishabh
Explanation
The next() method reads a single word from the user until a space is encountered.
Concepts Covered
- Scanner Class
- String Input
- next() Method
- User Input
4. Java Program to Read a Full Sentence Using nextLine()
Problem Statement
Write a Java program to accept a complete sentence from the user using the nextLine() method and display it.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String sentence;
System.out.print("Enter a Sentence: ");
sentence = scanner.nextLine();
System.out.println("You Entered:");
System.out.println(sentence);
scanner.close();
}
}
Sample Input
Enter a Sentence: Java is an object oriented programming language.
Sample Output
You Entered:
Java is an object oriented programming language.
Explanation
The nextLine() method reads the entire line entered by the user, including spaces, until the Enter key is pressed.
Difference Between next() and nextLine()
| Method | Reads |
|---|---|
next() | Only one word (stops at space) |
nextLine() | Complete line including spaces |
Concepts Covered
- Scanner Class
- nextLine() Method
- String Input
- User Input
5. Java Program to Read a Character from the User
Problem Statement
Write a Java program to accept a character from the user and display it.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char character;
System.out.print("Enter a Character: ");
character = scanner.next().charAt(0);
System.out.println("Character Entered = " + character);
scanner.close();
}
}
Sample Input
Enter a Character: A
Sample Output
Character Entered = A
Explanation
The Scanner class does not provide a direct method to read a character.
Instead:
next()reads the first word.charAt(0)extracts the first character of that word.
Example:
Input:
Coding
Output:
C
Concepts Covered
- Scanner Class
- Character Input
- charAt() Method
- String Handling
6. Java Program to Read Multiple Inputs Using Scanner
Problem Statement
Write a Java program to accept a student’s name, age, and percentage using the Scanner class and display the entered information.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
int age;
double percentage;
System.out.print("Enter Name: ");
name = scanner.nextLine();
System.out.print("Enter Age: ");
age = scanner.nextInt();
System.out.print("Enter Percentage: ");
percentage = scanner.nextDouble();
System.out.println("\nStudent Details");
System.out.println("-----------------------");
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Percentage : " + percentage);
scanner.close();
}
}
Sample Input
Enter Name: Rishabh Kumar
Enter Age: 22
Enter Percentage: 88.5
Sample Output
Student Details
-----------------------
Name : Rishabh Kumar
Age : 22
Percentage : 88.5
Explanation
This program demonstrates how to read different types of user input in a single program using the Scanner class.
Concepts Covered
- Scanner Class
- Multiple User Inputs
- String Input
- Integer Input
- Double Input
7. Java Program to Display Formatted Output Using printf()
Problem Statement
Write a Java program to display formatted output using the printf() method.
Java Solution
public class Main {
public static void main(String[] args) {
String name = "Rishabh";
int age = 22;
double percentage = 91.75;
System.out.printf("Name : %s%n", name);
System.out.printf("Age : %d%n", age);
System.out.printf("Percentage : %.2f%n", percentage);
}
}
Sample Output
Name : Rishabh
Age : 22
Percentage : 91.75
Explanation
The printf() method formats the output using format specifiers.
Common Format Specifiers
| Specifier | Description |
|---|---|
%d | Integer |
%f | Floating-point number |
%.2f | Decimal number with 2 digits after the decimal |
%s | String |
%c | Character |
%n | New line |
Concepts Covered
- printf()
- Formatted Output
- Format Specifiers
8. Java Program to Read Data Using BufferedReader
Problem Statement
Write a Java program to accept a string using the BufferedReader class.
Java Solution
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter Your Name: ");
String name = reader.readLine();
System.out.println("Welcome " + name);
}
}
Sample Input
Enter Your Name: Java Learner
Sample Output
Welcome Java Learner
Explanation
BufferedReader reads an entire line of text using the readLine() method. It is generally faster than Scanner for large input.
Concepts Covered
- BufferedReader
- InputStreamReader
- readLine()
- IOException
9. Java Program to Read an Integer Using BufferedReader
Problem Statement
Write a Java program to accept an integer using BufferedReader.
Java Solution
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter an Integer: ");
int number = Integer.parseInt(reader.readLine());
System.out.println("Number = " + number);
}
}
Sample Input
Enter an Integer: 120
Sample Output
Number = 120
Explanation
Since readLine() always returns a String, the Integer.parseInt() method converts it into an integer.
Concepts Covered
- BufferedReader
- Integer.parseInt()
- String to Integer Conversion
10. Java Program to Read a Double Using BufferedReader
Problem Statement
Write a Java program to accept a decimal number using BufferedReader.
Java Solution
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter Decimal Number: ");
double number =
Double.parseDouble(reader.readLine());
System.out.println("Number = " + number);
}
}
Sample Input
Enter Decimal Number: 99.75
Sample Output
Number = 99.75
Explanation
Double.parseDouble() converts the string returned by readLine() into a double value.
Concepts Covered
- BufferedReader
- Double.parseDouble()
- String to Double Conversion
- User Input
11. Java Program to Read Multiple Numbers Using BufferedReader
Problem Statement
Write a Java program to accept two integers using BufferedReader and display their sum.
Java Solution
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter First Number: ");
int firstNumber = Integer.parseInt(reader.readLine());
System.out.print("Enter Second Number: ");
int secondNumber = Integer.parseInt(reader.readLine());
int sum = firstNumber + secondNumber;
System.out.println("Sum = " + sum);
}
}
Sample Input
Enter First Number: 25
Enter Second Number: 35
Sample Output
Sum = 60
Explanation
The program accepts two integers using BufferedReader, converts them into integers using Integer.parseInt(), and calculates their sum.
Concepts Covered
- BufferedReader
- Integer.parseInt()
- Arithmetic Operations
- User Input
12. Java Program to Calculate the Sum Using Scanner Input
Problem Statement
Write a Java program to accept three numbers using the Scanner class and display their sum.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int firstNumber, secondNumber, thirdNumber;
System.out.print("Enter First Number: ");
firstNumber = scanner.nextInt();
System.out.print("Enter Second Number: ");
secondNumber = scanner.nextInt();
System.out.print("Enter Third Number: ");
thirdNumber = scanner.nextInt();
int sum = firstNumber + secondNumber + thirdNumber;
System.out.println("Sum = " + sum);
scanner.close();
}
}
Sample Input
Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 30
Sample Output
Sum = 60
Explanation
The Scanner class accepts multiple integer inputs and stores them in variables before calculating the total.
Concepts Covered
- Scanner Class
- Multiple Inputs
- Variables
- Arithmetic Operations
13. Java Program to Display User Details
Problem Statement
Write a Java program to accept and display user details such as name, age, city, and salary.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
String city;
int age;
double salary;
System.out.print("Enter Name: ");
name = scanner.nextLine();
System.out.print("Enter Age: ");
age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter City: ");
city = scanner.nextLine();
System.out.print("Enter Salary: ");
salary = scanner.nextDouble();
System.out.println("\nEmployee Details");
System.out.println("-----------------------");
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("City : " + city);
System.out.println("Salary : " + salary);
scanner.close();
}
}
Sample Input
Enter Name: Rahul Sharma
Enter Age: 25
Enter City: Delhi
Enter Salary: 45000
Sample Output
Employee Details
-----------------------
Name : Rahul Sharma
Age : 25
City : Delhi
Salary : 45000.0
Explanation
This program demonstrates reading multiple data types using a single Scanner object.
The extra scanner.nextLine() is used to consume the newline character left after reading the integer.
Concepts Covered
- Scanner Class
- Multiple Data Types
- String Input
- nextLine()
14. Java Program to Read Mixed Data Types
Problem Statement
Write a Java program to accept an integer, double, character, and string from the user.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
double marks;
char grade;
String name;
System.out.print("Enter Age: ");
age = scanner.nextInt();
System.out.print("Enter Marks: ");
marks = scanner.nextDouble();
System.out.print("Enter Grade: ");
grade = scanner.next().charAt(0);
scanner.nextLine();
System.out.print("Enter Name: ");
name = scanner.nextLine();
System.out.println("\nStudent Details");
System.out.println("--------------------");
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Marks : " + marks);
System.out.println("Grade : " + grade);
scanner.close();
}
}
Sample Input
Enter Age: 20
Enter Marks: 89.5
Enter Grade: A
Enter Name: Aman Gupta
Sample Output
Student Details
--------------------
Name : Aman Gupta
Age : 20
Marks : 89.5
Grade : A
Explanation
This program demonstrates how to read different data types in a single Java program.
Concepts Covered
- Scanner Class
- Mixed Data Types
- Character Input
- String Input
15. Java Program to Demonstrate print(), println(), and printf()
Problem Statement
Write a Java program to demonstrate the difference between print(), println(), and printf().
Java Solution
public class Main {
public static void main(String[] args) {
System.out.print("Java ");
System.out.print("Programming");
System.out.println();
System.out.println("Input Output");
System.out.printf("Percentage = %.2f", 92.567);
}
}
Sample Output
Java Programming
Input Output
Percentage = 92.57
Explanation
print()
Displays text without moving to the next line.
println()
Displays text and moves the cursor to the next line.
printf()
Displays formatted output using format specifiers.
Concepts Covered
- print()
- println()
- printf()
- Formatted Output
- Java Output Methods
Chapter Summary
In this chapter, you learned the fundamentals of Java Input and Output (I/O), one of the most important concepts in Java programming. You practiced accepting user input using the Scanner class and BufferedReader, displaying output using print(), println(), and printf(), and handling different data types such as integers, doubles, characters, strings, and mixed inputs.
You also learned how to format output, convert strings into numeric values using parsing methods, and read complete sentences from the user. These skills are essential for building interactive Java applications and are widely used in real-world software development.
Mastering Java Input and Output prepares you for advanced topics such as Conditional Statements, Loops, Methods, Object-Oriented Programming (OOP), File Handling, JDBC, and Spring Boot.
Key Takeaways
- The
Scannerclass is the easiest way to accept user input in Java. BufferedReaderprovides faster input for larger applications.next(),nextLine(),nextInt(), andnextDouble()read different types of input.print(),println(), andprintf()display output in different formats.printf()supports formatted output using format specifiers.- Parsing methods such as
Integer.parseInt()convert strings into numeric values. - Handling mixed data types requires proper management of the input buffer.
- Java Input and Output concepts are used in almost every Java application.
- Understanding I/O improves programming logic and problem-solving skills.
- Strong I/O knowledge is essential for coding interviews and real-world Java development.
Frequently Asked Questions (FAQs)
1. What is Input and Output in Java?
Input refers to accepting data from the user, while output refers to displaying information on the screen.
2. Which class is commonly used for user input in Java?
The Scanner class from the java.util package is the most commonly used class for reading user input.
Example:
Scanner scanner = new Scanner(System.in);
3. What is the difference between next() and nextLine()?
| Method | Description |
|---|---|
next() | Reads only one word until a space is encountered. |
nextLine() | Reads the entire line including spaces. |
4. Why is BufferedReader faster than Scanner?
BufferedReader reads larger blocks of data at once, making it faster than Scanner for large input and competitive programming.
5. What is the purpose of printf()?
The printf() method displays formatted output using format specifiers.
Example:
System.out.printf("%.2f", 95.678);
Output:
95.68
6. Why do we use Integer.parseInt()?
BufferedReader.readLine() returns a string. Integer.parseInt() converts that string into an integer.
Example:
int number = Integer.parseInt(reader.readLine());
7. Why is scanner.nextLine() used after nextInt()?
After reading an integer, the newline character remains in the input buffer. Calling nextLine() consumes this leftover newline before reading the next string.
8. Why are Java Input and Output concepts important?
They allow Java applications to interact with users, process data, display results, and form the basis for almost every console, desktop, Android, and enterprise application.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
