0

Could you help me understand how to get the same result by usint \n?

System.out.println("Balance: " + balance);
System.out.println();

I have tried something like

System.out.println("Balance: " + balance +\n);

Not working. Don't know whether it is possible or not.

Kifsif
  • 3,127
  • 10
  • 33
  • 41

6 Answers6

3
System.out.println("Balance: " + balance +"\n");
Amilcar Andrade
  • 1,121
  • 9
  • 16
1

Use

System.out.println("Balance: " + balance +"\n");
rocketboy
  • 9,277
  • 1
  • 33
  • 36
1

try

System.out.println("Balance: " + balance +"\n");
Zennichimaro
  • 4,986
  • 5
  • 48
  • 76
0

You need to include "\n" in quotes. The "\" operator is to ignore the next operator, and n stands for new line. If you just put "n", it will be a string output of "n".

System.out.println("Balance: " + balance +"\n"); gives you:

Balance: *value* 

Next output will be here
wchargin
  • 15,041
  • 11
  • 67
  • 107
user3871
  • 11,764
  • 27
  • 118
  • 244
0

Put "\n" at the end of the string:

System.out.println("Balance: " + balance + "\n");

This works because \n is a special character that stands for newline. It's platform-independent; you won't need to use \r\n or anything like that. You can use \n anywhere a string is accepted.

However, in this case, I would actually favor the first approach, because it makes it clear what you're trying to do. A call to println() is much clearer than a \n subtly tacked on at the end there.

wchargin
  • 15,041
  • 11
  • 67
  • 107
0

\n is the escape sequence for newline character and must be used as a constant of type char. So code will be:

System.out.println("Balance: " + balance + "\n");

or

System.out.println("Balance: " + balance +'\n');

There are other escape chars as \t (tab), \b etc Loeek here for full list and explanation

Luca Basso Ricci
  • 17,015
  • 2
  • 43
  • 65