Input and Output in Java
Introduction
Input and Output (I/O) is used to communicate between a Java program and the user or other data sources.
- Input means receiving data from the user or another source.
- Output means displaying or sending data from the program.
For example:
Input → User enters name ↓ Java Program ↓ Output → Hello Anuradha
1. Output in Java
Java provides several ways to display output.
The most commonly used methods are:
System.out.print()System.out.println()System.out.printf()
1.1 System.out.print()
print() displays output without moving to a new line.
Example
public class Main { public static void main(String[] args) { System.out.print("Hello "); System.out.print("Java"); } }
Output:
Hello Java
1.2 System.out.println()
println() displays output and moves the cursor to the next line.
Example
public class Main { public static void main(String[] args) { System.out.println("Hello"); System.out.println("Java"); } }
Output:
Hello Java
1.3 System.out.printf()
printf() is used to display formatted output.
Example
public class Main { public static void main(String[] args) { String name = "Anuradha"; int age = 20; System.out.printf("Name: %s%n", name); System.out.printf("Age: %d%n", age); } }
Output:
Name: Anuradha Age: 20
Common Format Specifiers
| Specifier | Used For |
|---|---|
%d | Integer |
%f | Floating-point number |
%s | String |
%c | Character |
%b | Boolean |
%n | New line |
2. Input in Java
Input allows a program to receive information from the user.
One of the easiest ways to take input is by using the Scanner class.
First, import it:
import java.util.Scanner;
3. Scanner Class
The Scanner class is available in the java.util package.
Basic Syntax
Scanner sc = new Scanner(System.in);
Here:
Scanner→ Class name.sc→ Scanner object.System.in→ Standard input, usually the keyboard.
4. Taking String Input
Using nextLine()
nextLine() reads a complete line of text.
import java.util.Scanner; public class Main { 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("Your name is: " + name); sc.close(); } }
Example:
Enter your name: Anuradha Your name is: Anuradha
Using next()
next() reads a single token, usually one word.
Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.next(); System.out.println("Name: " + name);
If the user enters:
Anuradha Mandal
next() reads only:
Anuradha
For a complete name or sentence, use nextLine().
5. Taking Integer Input
Use nextInt() to read an integer.
import java.util.Scanner; public class Main { 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("Age: " + age); sc.close(); } }
6. Taking Double Input
Use nextDouble() to read a decimal number.
Scanner sc = new Scanner(System.in); System.out.print("Enter your salary: "); double salary = sc.nextDouble(); System.out.println("Salary: " + salary); sc.close();
7. Taking Float Input
Use nextFloat() for a float value.
Scanner sc = new Scanner(System.in); System.out.print("Enter price: "); float price = sc.nextFloat(); System.out.println("Price: " + price); sc.close();
8. Taking Character Input
There is no nextChar() method in Scanner.
We can read a character using next().charAt(0).
Scanner sc = new Scanner(System.in); System.out.print("Enter your grade: "); char grade = sc.next().charAt(0); System.out.println("Grade: " + grade); sc.close();
If the user enters:
A
The output is:
Grade: A
9. Taking Boolean Input
Use nextBoolean().
Scanner sc = new Scanner(System.in); System.out.print("Are you a student? "); boolean student = sc.nextBoolean(); System.out.println("Student: " + student); sc.close();
Input:
true
Output:
Student: true
10. Common Scanner Methods
| Method | Input Type |
|---|---|
next() | One word/token |
nextLine() | Complete line |
nextInt() | Integer |
nextLong() | Long integer |
nextFloat() | Float |
nextDouble() | Double |
nextBoolean() | Boolean |
next().charAt(0) | Character |
11. Taking Multiple Inputs
We can take multiple inputs in one program.
import java.util.Scanner; public class Main { 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.print("Enter your GPA: "); double gpa = sc.nextDouble(); System.out.println("\n--- Student Details ---"); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("GPA: " + gpa); sc.close(); } }
Example output:
Enter your name: Anuradha Mandal Enter your age: 20 Enter your GPA: 3.36 --- Student Details Name: Anuradha Mandal Age: 20 GPA: 3.36
12. Important Scanner Problem
A common problem occurs when nextInt() or another numeric method is followed by nextLine().
Example:
int age = sc.nextInt(); String name = sc.nextLine();
The nextLine() may read the remaining newline instead of the expected text.
Solution
Use an extra nextLine():
int age = sc.nextInt(); sc.nextLine(); String name = sc.nextLine();
13. Command-Line Input
Java can also receive input through command-line arguments.
Example:
public class Main { public static void main(String[] args) { System.out.println("Hello " + args[0]); } }
Run:
java Main Anuradha
Output:
Hello Anuradha
Here, args[0] contains the first command-line argument.
14. Output Using Variables
Variables can be combined with output.
public class Main { public static void main(String[] args) { String name = "Anuradha"; int age = 20; System.out.println("My name is " + name); System.out.println("My age is " + age); } }
Output:
My name is Anuradha My age is 20
The + operator is used to concatenate text and values.
15. Complete Input and Output Example
import java.util.Scanner; public class StudentInfo { 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.print("Enter your GPA: "); double gpa = sc.nextDouble(); System.out.println("\n===== Student Information ====="); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("GPA: " + gpa); sc.close(); } }
Example:
Enter your name: Anuradha Mandal Enter your age: 20 Enter your GPA: 3.36 ===== Student Information ===== Name: Anuradha Mandal Age: 20 GPA: 3.36
16. Input and Output Flow
User | | Input ↓ Scanner / System.in | ↓ Java Program | | Output ↓ System.out.println() | ↓ Screen
Summary
Output
System.out.print(); System.out.println(); System.out.printf();
Input
Scanner sc = new Scanner(System.in); sc.next(); sc.nextLine(); sc.nextInt(); sc.nextLong(); sc.nextFloat(); sc.nextDouble(); sc.nextBoolean();
Conclusion
Input and output are fundamental parts of Java programming. Scanner makes it easy to receive input from the keyboard, while System.out provides simple methods for displaying output. Understanding these concepts allows you to create interactive Java programs that can communicate with users.