Print output and an error message to the console

System.out.println() and System.err.println() are different Streams of execution. Output Streams are cached so all the write goes into this memory buffer. After a period of quiet, they are actually written out. Here is a for loop that essentially shows your error again:

for(int x = 0; x <= 5; x++) {
    System.out.println("Out");
    System.err.println("Err");



}

In order to "flush" the Streams call .flush() each time through the loop:

for(int x = 0; x <= 5; x++) {
    System.out.println("Out");
    System.out.flush();
    System.err.println("Err");
    System.err.flush();


}

In this for loop, the out message, and the err message will initially print, but on each flush your out messages would print first, and then your err messages. The output will be something such as:

OutErr
Out
Out
Out
Out
Out

Err
Err
Err
Err
Err

And that is because System.out and System.err are executing on different Streams.

Tags:

Java

Eclipse