4

I have done some research concerning System.out.print() and System.out.println() and I discovered that System.out.println() add the end of line at the end of printed line.

System.out.println("Test");

Output only :

Test

but does not print end of the line.

System.out.print("Test");

Output only:

Test

but does not end the line and leave some place for other words or numbers, etc etc.

A more illustrative way is:

Test_____________________________________________ (All "blank" spots)

Is there a way to, force an end of line with System.out.print() directly after the word Test? Will the usage of % will remove the "blank" spots?

Or a way to code a function that will end the line after I used several System.out.print() to print a sentence?

For exemple :

System.out.print("Test);
System.out.print("Test);

will outpost a pure:

Test Test

like System.out.println("Test Test")

1ac0
  • 2,875
  • 3
  • 33
  • 47
metraon
  • 1,229
  • 2
  • 11
  • 26

6 Answers6

14

You can append line separator, Note that it is platform dependant, so :

  • Windows ("\r\n")
  • Unix/Linux/OSX ("\n")
  • pre-OSX Mac ("\r")

If you want to get line separator depending on actual system you can just use :

  • System.getProperty("line.separator"); for pre Java 7
  • System.lineSeparator(); for Java 7

Then you just append your separator to your string in System.out.print, like :

  • System.out.print("Word\n");
Fallup
  • 2,417
  • 19
  • 30
  • 1
    \n is not what I was thinking as I evaluated it in my tests. line.separator is really what I wanted. – metraon Jan 17 '13 at 16:42
4

You can do System.out.print("Test\n").

fo_x86
  • 2,583
  • 1
  • 30
  • 41
4

System.out.println("Test"); will print end of the line. When you don't see it in your IDE it doesn't mean it isn't here. Try:

System.out.println("Test1");
System.out.println("Test2");
System.out.println("Test3");

which will produce:

Test1
Test2
Test3

From the docu of the println(String):

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println()

and from the docu of the println():

Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
1ac0
  • 2,875
  • 3
  • 33
  • 47
3

For getting an OS independent EOL character you could try

System.getProperty("line.separator");

and then appending that to your string ?

Why do you want to manually do that though since there is a function to do it for you ?

Dio
  • 348
  • 2
  • 12
0

You can explicitly include a newline char in what you are printing:

System.out.print("Test\n");

This will force the newline while using the print function.

Mike Clark
  • 11,769
  • 6
  • 39
  • 43
0

You can force a manual newline like so:

System.out.print("Test\n");

You can also mix the two when you need to print then end the line like so:

System.out.print("This is on ");
System.out.print("the same line.");
System.out.println(" and now we'll end the line");
System.out.println("This is on a different line.");
Jesan Fafon
  • 2,234
  • 1
  • 25
  • 29