0
Please help me with "\n" meaning symbol
am taking MOOC course . I am at part 6 "Objects on a list and a list as part of an object". I have the following syntax: public String toString() { String onTheRide = ""; for (Person person: riding) { onTheRide = onTheRide + person.getName() + "\n"; } return "riding:\n" + onTheRide; } Question: How does \n is actually working on the each for loop? Why the names return by the getName() method are placed one bellow the other and not in a String line?
3 Réponses
+ 2
String onTheRide = "";
for (Person person: riding) {
onTheRide = onTheRide + person.getName() + "\n";
}
the name is going to be appended into the onTheRide string variable with an new line at the end of each name.
e.g
Smith
Jack
Tom
+ 2
\n means a line break
This means insert a newline in the text at this point.
example :
System.out.println("hello\nworld");
Output:
hello
world
0
i understand it, but how does it work in the for each loop?