Java Input and Output
Almost every program needs to interact with the user — either by displaying information (output) or by receiving information (input). Java provides several ways to handle both. Understanding input and output operations is essential for writing interactive programs.
Output in Java
Java uses the System.out object to send output to the console (the screen). There are three main methods for printing output:
1. System.out.println()
Prints text followed by a new line. The cursor moves to the next line after printing.
System.out.println("Hello, Java!");
System.out.println("This is on the next line.");Output:
Hello, Java!
This is on the next line.2. System.out.print()
Prints text without adding a new line. The next output continues on the same line.
System.out.print("Java ");
System.out.print("is ");
System.out.print("fun!");
System.out.println();Output:
Java is fun!3. System.out.printf()
Prints formatted output. It works like a template where placeholders are replaced by actual values.
String name = "Alice";
int age = 25;
double score = 98.5;
System.out.printf("Name: %s, Age: %d, Score: %.1f%n", name, age, score);Output:
Name: Alice, Age: 25, Score: 98.5Common Format Specifiers for printf
| Specifier | Data Type | Example |
|---|---|---|
| %d | Integer | %d → 42 |
| %f | Float / Double | %.2f → 3.14 |
| %s | String | %s → "Java" |
| %c | Character | %c → 'A' |
| %b | Boolean | %b → true |
| %n | New line | Platform-safe newline |
Input in Java – Using Scanner
The Scanner class is the most commonly used way to take input from the user in Java. It is part of the java.util package and must be imported before use.
Importing Scanner
import java.util.Scanner;Creating a Scanner Object
Scanner sc = new Scanner(System.in);System.in refers to the keyboard (standard input). The Scanner wraps around it to read what the user types.
Reading Different Types of Input
| Method | Reads |
|---|---|
| sc.nextInt() | An integer value |
| sc.nextDouble() | A double value |
| sc.nextFloat() | A float value |
| sc.nextLong() | A long value |
| sc.next() | A single word (no spaces) |
| sc.nextLine() | An entire line of text (including spaces) |
| sc.nextBoolean() | A boolean value (true/false) |
Example – Reading an Integer and a 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 + "! You are " + age + " years old.");
sc.close();
}
}Sample Interaction:
Enter your name: Alice
Enter your age: 22
Hello, Alice! You are 22 years old.Example – Reading Multiple Values
import java.util.Scanner;
public class CalculatorInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
double sum = num1 + num2;
System.out.printf("Sum: %.2f%n", sum);
sc.close();
}
}Sample Interaction:
Enter first number: 12.5
Enter second number: 7.3
Sum: 19.80Important Note: Mixing nextLine() with nextInt()
A common beginner pitfall occurs when nextLine() is used after nextInt() or nextDouble(). After reading a number, there is a leftover newline character in the input buffer. The next nextLine() reads that newline instead of the user's input.
The Problem
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
String name = sc.nextLine(); // This may read an empty string!The Fix – Add an extra nextLine() to consume the leftover newline
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
sc.nextLine(); // consume the leftover newline
System.out.print("Enter name: ");
String name = sc.nextLine(); // now reads correctly
System.out.println(name + " is " + age + " years old.");Closing the Scanner
It is good practice to call sc.close() after the Scanner is no longer needed. This releases the system resource associated with the input stream.
sc.close();Summary
System.out.println()prints output with a new line;print()without;printf()with formatting.- The
Scannerclass reads user input from the keyboard viaSystem.in. - Use
nextInt(),nextDouble(),next(), andnextLine()for different input types. - Always import
java.util.Scannerbefore using the Scanner class. - When mixing
nextInt()andnextLine(), add an extrasc.nextLine()to handle the leftover newline. - Close the Scanner with
sc.close()when done.
