-2

I know that

for

example

is printed by: System.out.print("for" + "\n" + "example") But what do I do when I want to print 2 blank lines instead of 1? Like this:

for


example

I tried System.out.print("for" + "\n" + "\n" + "example") but it still printed 1 blank line

user2489526
  • 161
  • 2
  • 3
  • 8

4 Answers4

11

System.out.print("for" + "\n\n\n" + "example") should solve your problem. The first "\n" is for ending "for" and then two blank lines

or

System.out.println("for");
System.out.println();
System.out.println();
System.out.println("example");
Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
Jens
  • 67,715
  • 15
  • 98
  • 113
1

You can use:

System.out.println("for");
System.out.println();
System.out.println();
System.out.println("example");

To print what you need.

System.out.println();

just prints an empty line.

As for your solution, you actually need 3 new line characters (one after "for" and two empty lines).

Your first statement is wrong, as:

System.out.print("for" + "\n" + "example")

will print:

for 
example
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

There are different ways of printing two newlines:

  1. Write an empty print statement twice or more:

    System.out.println();
    System.out.println();
    
  2. System.out.print( "\n\n\n" )

  3. Use a loop in case you want to use a single println and even without using /n:

    public void foo(int n) {
        if (n > 3) 
            return;
        println(currNum);
        foo(n+1);
    }
    
Jamal
  • 763
  • 7
  • 22
  • 32
moe
  • 25
  • 8
0

Using System.getProperty("line.separator") should return the operator you are looking for. My guess is that this property depends from OS'es. You can use this to print two separate lines:

System.out.prinln("line 1"+System.getProperty("line.separator")+"line 2");

This will print:

line 1
line 2
Freya
  • 302
  • 5
  • 10