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!

Recommended Answers

All 6 Replies

"\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 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.

Gimme a second and I'll print screen it and post a link to the picture

http://i898.photobucket.com/albums/ac184/srad1292/printscreenjav.png

There ^ :D

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.