Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Output · Lesson 19 of 28

Formatting Output

A free lesson from Swipe Coding · Java, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
You've basically got Java's whole output toolkit now — let's put it all together. 🧰

Now you will combine everything you have learned so far — println(), print(), and the escape characters \n, \t, and \" — to format output exactly the way you want it.

System.out.println("=== Shopping List ===");
System.out.println("Apples");
System.out.println("Bread");
System.out.println("Milk");

// Output:
// === Shopping List ===
// Apples
// Bread
// Milk
Four println() calls produce four lines — a simple, clear list.
🤖
One \n can replace four separate println() calls. ⚡

The same output can also be produced with just a single println() call if you use \n to separate the lines within the text.

System.out.println("=== Shopping List ===\nApples\nBread\nMilk");

// Output:
// === Shopping List ===
// Apples
// Bread
// Milk
A single call with \n produces exactly the same output as four separate println() calls.
True or false

System.out.println("A\nB"); produces the same output as two separate calls, System.out.println("A"); and System.out.println("B");.

With \t you can make multiple lines look like a small table by aligning text into columns.

System.out.println("Product\tPrice");
System.out.println("Apple\t€0.50");
System.out.println("Bread\t€2.00");

// Output:
// Product  Price
// Apple    €0.50
// Bread    €2.00
Each line begins with a word followed by \t — this makes the prices line up roughly in a column.
Quick check

Why do you use \t in multiple consecutive println() lines?

🤖
Combining print() and println() lets you build a line piece by piece. 🧩

You can also combine print() and println() to build a line out of multiple parts — for example a label, immediately followed by a quote in quotation marks.

System.out.print("Quote: ");
System.out.println("\"Talk is cheap. Show me the code.\"");

// Output:
// Quote: "Talk is cheap. Show me the code."
print() keeps the cursor on the same line, println() then closes it off. \" produces the actual quotation marks in the quote.
Fill in the blank

In the example above, ____ is used so that "Quote: " and the quote appear on the same line.

Quick check

What does this code output?

System.out.print("Result:\t");
System.out.println("42");
Chapter quiz

Output

10 questions · pass with 60% — just like in the app.

Quick check

What happens automatically after System.out.println("Hi"); outputs the text?

True or false

System.out.print() automatically adds a line break at the end.

Fill in the blank

The escape character ____ produces a line break in the middle of the text.

Quick check

What does this code output?

System.out.print("A ");
System.out.println("test");
True or false

\t produces a tab that helps align text into columns.

Fill in the blank

To output a quotation mark directly in the text, you write ____.

Quick check

Why do you have to write \\ to output a single backslash?

True or false

System.out.println("A\nB"); produces the same output as two separate println() calls for "A" and "B".

Quick check

What does this code output?

System.out.println("X");
System.out.println("Y");
Fill in the blank

With System.out.____(), the cursor stays on the same line after the output.

Keep your streak going in Swipe Coding

Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →