Okay so we are doing a BankAccount program in my CS class. One method is supposed to return the information like this -

Account Name: <name here>
Account Number: <number here>
Account Balance: <balance here>

The instructions say to return it on multiple lines using the \n command.

How do I use the \n command to return multiple lines?

I don't know why we can't just use the terminal window and then I could do System.out.println(), but oh well.

Any help would be appreciated!

"\n" isn't a command, it's just a new line character and you can use it in Strings just like you use "A" or "B". So, for example, the String "ABC\nDEF" is a valid String that when printed would look like this:
ABC
DEF


Of course you could print it to the terminal like that, but then it wouldn't be usable by any other part of your program. Return it as a String and you can still print it if you want, but you can also use it to (for example) store the values in a database.

"\n" isn't a command, it's just a new line character and you can use it in Strings just like you use "A" or "B". So, for example, the String "ABC\nDEF" is a valid String that when printed would look like this:
ABC
DEF


Of course you could print it to the terminal like that, but then it wouldn't be usable by any other part of your program. Return it as a String and you can still print it if you want, but you can also use it to (for example) store the values in a database.

Okay so like..

return "Account Name: " + name + "\n Account Number: " + accountNumber;

that just returns it on one line. What is it about the \n I'm doing wrong? Because I just put the \n in there like you did

That code looks fine to me.
It depends on what you mean by one line; it may be one String, but if you print it you will see that it prints on multiple lines.

That's an IDE display of the String value with newlines, tabs etc shown as characters (which is what you need in that context).
Try printing it, as in
System.out.print(" etc

Edited 5 Years Ago by JamesCherrill: n/a

Just got back from my CS class.

The professor said the way it looked in my prtscrn was right. There's a method later on that calls this one and prints it correctly to the terminal screen


So thank you JamesCherrill for your help because the way you had the \n was right :D

This question has already been answered. Start a new discussion instead.