Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Current Progress

nsay
nsay
6,709 Points

What does "%s" & "%n" mean in the displayProgress method?

In the displayProgress method (Prompter.java class), Craig has added a "%s%n" in the System.out.printf statement.

What does "%s" & "%n" mean?

1 Answer

Mohammad Laif
PLUS
Mohammad Laif
Courses Plus Student 22,297 Points

They are format specifiers used in some methods like printf() to format the string.

  • The %s is replaced with the times value (below in the example).
  • The %n tells the console print it in a new line.

Example:

        Integer times = 3;
        System.out.printf("How many times you should try: %s%n", times);
        times = times + 1;
        System.out.printf("How many times he should try: %s%n", times);

output would be like:

How many times you should try: 3
How many times he should try: 4

Example without %n: if we delete the %n it will print the output in the same line:

        Integer times = 3;
        System.out.printf("How many times you should try: %s", times);
        times = times + 1;
        System.out.printf("How many times he should try: %s", times);

output would be like:

How many times you should try: 3How many times he should try: 4
nsay
nsay
6,709 Points

Mohammad, thanks for the clear explanation!