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?

14th Sep 2021, 1:54 PM
Remus Tanasa
3 Respostas
+ 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
14th Sep 2021, 3:08 PM
Sacalivin Obiri
Sacalivin Obiri - avatar
+ 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
14th Sep 2021, 1:54 PM
Pariket Thakur
Pariket Thakur - avatar
0
i understand it, but how does it work in the for each loop?
14th Sep 2021, 2:04 PM
Remus Tanasa