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")

4

6 Answers 6

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");
1
  • 1
    \n is not what I was thinking as I evaluated it in my tests. line.separator is really what I wanted.
    – metraon
    Commented Jan 17, 2013 at 16:42
4

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

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').

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 ?

1
  • 2
    He can just use System.lineSeparator(); in Java 7.
    – Fallup
    Commented Jan 17, 2013 at 16:31
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.

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.");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.