Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Formatting with Flags and Special Characters | Mastering String.format()
Quizzes & Challenges
Quizzes
Challenges
/
Formatting & Parsing in Java

bookFormatting with Flags and Special Characters

When you want to control the appearance of numbers and text in your Java output, String.format() offers a set of flags and escape sequences that help you fine-tune how your data is presented. Flags are special characters that modify the behavior of format specifiers, while escape sequences allow you to insert special characters such as newlines or tabs into your output.

Some of the most commonly used flags include:

  • The + flag: always adds a plus sign before positive numbers and a minus sign before negative numbers;
  • The 0 flag: pads numbers with leading zeros instead of spaces;
  • The - flag: left-justifies the output within the specified width;
  • The , flag: adds grouping separators, such as commas, to large numbers.

For escape sequences, the most frequently used are:

  • \n: inserts a new line;
  • \t: inserts a tab character.

These features can be combined to create readable, well-structured output, whether you are displaying tables, aligning numbers, or generating logs.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; public class Main { public static void main(String[] args) { int positive = 42; int negative = -42; int small = 7; // Using '+' flag to show sign, and '0' flag to pad with zeros String formattedPositive = String.format("%+05d", positive); String formattedNegative = String.format("%+05d", negative); String formattedSmall = String.format("%05d", small); System.out.println("Positive with sign and zero padding: " + formattedPositive); System.out.println("Negative with sign and zero padding: " + formattedNegative); System.out.println("Small number with zero padding: " + formattedSmall); } }

Escape sequences can be used together with formatted strings to produce structured, easy-to-read output. When you combine format specifiers with \n for new lines or \t for tabs, you can create tables or multi-line displays that improve the clarity of your program's output. This is especially helpful when you want to align columns or separate sections visually in the console.

Main.java

Main.java

copy
12345678910111213
package com.example; public class Main { public static void main(String[] args) { String header = String.format("Name\t\tScore\n"); String row1 = String.format("%-10s\t%d\n", "Alice", 95); String row2 = String.format("%-10s\t%d\n", "Bob", 87); String row3 = String.format("%-10s\t%d\n", "Charlie", 78); System.out.print(header + row1 + row2 + row3); } }

1. What does the '+' flag do when formatting numbers with String.format()?

2. Fill in the blank: To pad an integer with zeros to a width of 5, use the format specifier ______.

3. Which escape sequence is used to insert a new line in formatted output?

question mark

What does the '+' flag do when formatting numbers with String.format()?

Select the correct answer

question-icon

Fill in the blank: To pad an integer with zeros to a width of 5, use the format specifier ______.

00042

Click or drag`n`drop items and fill in the blanks

question mark

Which escape sequence is used to insert a new line in formatted output?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me an example of using flags and escape sequences together in Java?

What are some common mistakes to avoid when using String.format() with flags and escape sequences?

How do I align numbers and text in columns using String.format()?

bookFormatting with Flags and Special Characters

Desliza para mostrar el menú

When you want to control the appearance of numbers and text in your Java output, String.format() offers a set of flags and escape sequences that help you fine-tune how your data is presented. Flags are special characters that modify the behavior of format specifiers, while escape sequences allow you to insert special characters such as newlines or tabs into your output.

Some of the most commonly used flags include:

  • The + flag: always adds a plus sign before positive numbers and a minus sign before negative numbers;
  • The 0 flag: pads numbers with leading zeros instead of spaces;
  • The - flag: left-justifies the output within the specified width;
  • The , flag: adds grouping separators, such as commas, to large numbers.

For escape sequences, the most frequently used are:

  • \n: inserts a new line;
  • \t: inserts a tab character.

These features can be combined to create readable, well-structured output, whether you are displaying tables, aligning numbers, or generating logs.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; public class Main { public static void main(String[] args) { int positive = 42; int negative = -42; int small = 7; // Using '+' flag to show sign, and '0' flag to pad with zeros String formattedPositive = String.format("%+05d", positive); String formattedNegative = String.format("%+05d", negative); String formattedSmall = String.format("%05d", small); System.out.println("Positive with sign and zero padding: " + formattedPositive); System.out.println("Negative with sign and zero padding: " + formattedNegative); System.out.println("Small number with zero padding: " + formattedSmall); } }

Escape sequences can be used together with formatted strings to produce structured, easy-to-read output. When you combine format specifiers with \n for new lines or \t for tabs, you can create tables or multi-line displays that improve the clarity of your program's output. This is especially helpful when you want to align columns or separate sections visually in the console.

Main.java

Main.java

copy
12345678910111213
package com.example; public class Main { public static void main(String[] args) { String header = String.format("Name\t\tScore\n"); String row1 = String.format("%-10s\t%d\n", "Alice", 95); String row2 = String.format("%-10s\t%d\n", "Bob", 87); String row3 = String.format("%-10s\t%d\n", "Charlie", 78); System.out.print(header + row1 + row2 + row3); } }

1. What does the '+' flag do when formatting numbers with String.format()?

2. Fill in the blank: To pad an integer with zeros to a width of 5, use the format specifier ______.

3. Which escape sequence is used to insert a new line in formatted output?

question mark

What does the '+' flag do when formatting numbers with String.format()?

Select the correct answer

question-icon

Fill in the blank: To pad an integer with zeros to a width of 5, use the format specifier ______.

00042

Click or drag`n`drop items and fill in the blanks

question mark

Which escape sequence is used to insert a new line in formatted output?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
some-alt