Welcome to the Treehouse Community
Looking to learn something new?
Ranak Bansal1,207 Points
Why did the teacher use %d and %n instead of %s and /n?
In previous lessons the teacher always used %s (now he used %d) when inserting a variable and /n (now he used %n) when creating a new line. How come he switched? Did he talk about this in a previous lesson that I missed, or what?
Cam RichardsonFront End Web Development Treehouse Moderator 16,894 Points
I believe he briefly explains the difference in a later lesson, but I can provide some explanation for reference.
%d is for when you're referencing a number in the formatted string specifically. Likewise %s is for String variables.
For the newline characters each OS handles them differently. *nix based systems (MacOS, Linux, etc.) expect a \n to denote a newline. Windows actually expects something different: \r\n. So when you type \n, you run the risk of having your string being incorrectly formatted on other platforms. Java has a built in solution for this in formatted strings which is %n. This automatically adds the platform appropriate newline character sequence for you so you don't have to worry about it.
Hope that helps!
Thanks for the info. I found your explanation very useful! :)