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 For Loops

Canbin Mei
Canbin Mei
428 Points

what are they? %d and %n

import java.io.Console;

public class forLoop { public static void main(String[] args) { Console console = System.console(); for (int i = 7; i > 0; i--) { console.printf("%d bottles of milk on the wall... %n",i); } } }

Output 7 bottles of milk on the wall... 6 bottles of milk on the wall... 5 bottles of milk on the wall... 4 bottles of milk on the wall... 3 bottles of milk on the wall... 2 bottles of milk on the wall... 1 bottles of milk on the wall...

What do %d and %n mean?

1 Answer

Rebecca Rich
PLUS
Rebecca Rich
Courses Plus Student 8,592 Points

%n is a placeholder for a new line (it will put a line break in the console print)

%d is a place holder for an integer (in this case, i will be placed into that point in the string)

console.printf("%d bottles of milk on the wall... %n",i);

if i is 7 will print the following to the console:

7 bottles of milk on the wall...

followed by a line break so the next loop will print to the next line in the console.

Canbin Mei
Canbin Mei
428 Points

Got it, thank you!