By the end of this module, you will be able to:
System.out.println, System.out.print, and System.out.printf appropriatelyprintf with common format specifiersScannernextLine, nextInt, and nextDouble to capture different input typesScanner newline quirk confidentlyJava gives you three methods for printing to the terminal. Choosing the right one keeps your output clean and predictable.
System.out.println — print and move to next lineThe one you've used most so far. Prints the value and adds a newline at the end.
System.out.println("Hello");
System.out.println("World");
// Hello
// World
System.out.print — print without a newlineOutput stays on the same line. The next print continues from where this one left off.
System.out.print("Hello");
System.out.print(", ");
System.out.print("World");
// Hello, World
System.out.printf — formatted printThe most powerful option. Lets you control exactly how values are embedded and displayed in a string using format specifiers. No automatic newline — you add \\\\n yourself when needed.
System.out.printf("Hello, %s! You are %d years old.%n", "Alice", 28);
// Hello, Alice! You are 28 years old.
💡
%nis the platform-safe newline forprintf— it works correctly on Windows, Mac, and Linux. You can also use\\\\nbut%nis preferred in formatted strings.
printfprintf works by placing format specifiers as placeholders inside a string. Each specifier is replaced by the corresponding argument in order.
System.out.printf("Name: %s, Age: %d, Score: %.2f%n", "Bob", 22, 94.5678);
// Name: Bob, Age: 22, Score: 94.57
| Specifier | Type | Example | Output |
|---|---|---|---|
%s |
String | printf("%s", "Alice") |
Alice |
%d |
Integer (int, long) |
printf("%d", 42) |
42 |
%f |
Decimal (double, float) |
printf("%f", 3.14) |
3.140000 |
%.2f |
Decimal, 2 decimal places | printf("%.2f", 3.14159) |
3.14 |
%b |
Boolean | printf("%b", true) |
true |
%c |
Character | printf("%c", 'A') |
A |
%n |
Newline | printf("Hi%n") |
Hi + newline |
You can pad output to a fixed width — useful for building aligned tables:
System.out.printf("%-15s %5d %8.2f%n", "Apple", 120, 1.50);
System.out.printf("%-15s %5d %8.2f%n", "Banana", 85, 0.75);
System.out.printf("%-15s %5d %8.2f%n", "Blueberry", 200, 3.99);
// Apple 120 1.50
// Banana 85 0.75
// Blueberry 200 3.99
%-15s — left-align a String in a 15-character wide column%5d — right-align an integer in a 5-character wide column%8.2f — right-align a decimal in an 8-character wide column, 2 decimal places🔍 Coming from JS,
printfis similar to template literals but more explicit about types and alignment. There's no direct equivalent in vanilla JS — you'd typically use a library or manual padding for formatted tables.
ScannerTo read input from the terminal, Java uses the Scanner class from java.util. You need to import it and create one Scanner object connected to System.in (the keyboard).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close(); // good practice — release the resource when done
}
}
Use the right method for the type of input you expect:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // reads a full line of text
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // reads an integer
System.out.print("Enter your height (m): ");
double height = scanner.nextDouble(); // reads a decimal number
System.out.printf("Name: %s | Age: %d | Height: %.2fm%n", name, age, height);
| Method | Reads | Notes |
|---|---|---|
nextLine() |
Full line (including spaces) | Reads until Enter |
nextInt() |
Integer only | Stops at whitespace |
nextDouble() |
Decimal number | Stops at whitespace |
next() |
Single word | Stops at whitespace |
Scanner Newline QuirkThis is one of the most common gotchas for beginners. When you call nextInt() or nextDouble(), Scanner reads the number but leaves the newline character (\\\\n) from pressing Enter still waiting in the input buffer.
The very next nextLine() call will immediately consume that leftover newline and return an empty string — skipping your input prompt entirely.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // reads "25", leaves "\\\\n" in buffer
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // ⚠️ instantly reads the leftover "\\\\n"
// name = "" — user never got to type!
System.out.println("Name: " + name); // Name: ← empty!
Call scanner.nextLine() immediately after nextInt() or nextDouble() to flush the buffer before reading a String:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine(); // ✅ flush the leftover newline
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // now reads correctly
System.out.printf("Name: %s, Age: %d%n", name, age);
💡 A simple rule: always add
scanner.nextLine()afternextInt()ornextDouble()if you plan to read a String next. Make it a habit and you'll never hit this bug.
Put everything in this module together to build a small interactive CLI program.
String)int)String)double)int)╔══════════════════════════════════════╗
PERSONAL PROFILE CARD
╚══════════════════════════════════════╝
Name : Alice van der Berg
Age : 31
City : Amsterdam
Rate : €22.50 / hr
Hours : 160 hrs
Monthly Pay : €3,600.00
══════════════════════════════════════
printf with format specifiers for the profile card outputScanner newline quirk correctly (city comes after age)Add input validation: if the user enters an age below 0 or above 120, print an error message and ask again. Hint: you'll need a loop from Module 4 — Control Flow.
println adds a newline, print does not, printf gives you full control over formattingprintf uses format specifiers — %s for Strings, %d for integers, %.2f for decimals with 2 decimal placesScanner connects your program to keyboard input via System.inScanner method for the type you're reading: nextLine(), nextInt(), nextDouble()scanner.nextLine() after nextInt() or nextDouble() when a String read followsscanner.close() when you're done