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,477
  • 10
  • 36
  • 45

6 Answers6

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

Use

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

try

System.out.println("Balance: " + balance +"\n");
Zennichimaro
  • 5,236
  • 6
  • 54
  • 78
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,589
  • 12
  • 71
  • 110
user3871
  • 12,432
  • 33
  • 128
  • 268
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,589
  • 12
  • 71
  • 110
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,829
  • 2
  • 47
  • 69