0

I thought that difference between System.out.println() and System.err.println() is simply in how messages are printed to console. Meaning err prints error messages (red color), while out prints standard messages. It has come to my notice that they are less alike than expected (for example, err messages would be always printed before out messages).

But there is one example I am particularly confused about. Say I have this simple program:

Scanner scanner = new Scanner(System.in);

while (true) {
    int num = scanner.nextInt();
    if (num > 0) {
        System.out.println("All good, bye");
        break;
    } else {
        System.out.println("Number must be possitive!");
        System.out.print("New try:");
    }
}

OUTPUT (from my testing):

enter image description here


But when I make literally same code and just change message "Number must be possitive" to be printed using System.err.println():

Scanner scanner = new Scanner(System.in);

while (true) {
    int num = scanner.nextInt();
    if (num > 0) {
        System.out.println("All good, bye");
        break;
    } else {
        System.err.println("Number must be possitive!");
        System.out.print("New try:");
    }
}

OUTPUT (from my testing):

enter image description here

I can't figure out how it is making these weird prints. Can someone tell me what am I missing?

Stefan
  • 969
  • 6
  • 9
  • Add `System.err.flush();` after `System.err.println("Number must be possitive!");`. –  Mar 27 '21 at 08:33
  • @saka1029 It doesn't help – Stefan Mar 27 '21 at 08:40
  • 1
    This is a problem cause by your IDE. If you run the code in a command prompt / terminal window, it will print correctly. Don't mix output to stdout and stderr when the output needs to be read by another program (like an IDE). – Andreas Mar 27 '21 at 09:13
  • 1
    `System.out` is buffered: `System.err` is not. – user207421 Mar 27 '21 at 09:13
  • @user207421 are you sure that is (always(even?)) the case? eclipse is using a `BufferedOutputStream` for **both** here - same also true for command line Java (Java 1.8.0_202) –  Mar 27 '21 at 10:01
  • There are a couple of possible explanations, depending on what provides the console you are writing to; e.g. an IDE, a terminal emulator, etc. But the bottom line is that the interspersing of `out` and `err` output is not 100% reliable across all platforms. This is not actually Java's fault. See the dup links for more information. – Stephen C Mar 27 '21 at 10:05
  • Positive*. 1 "s". – Det Jan 27 '22 at 05:18

2 Answers2

1

I think it's because two separate streams writing to same console.

int i = 5;
while (--i>0) {
        System.out.println("Number must be possitive!");
        System.out.println("New try:");
}

System.out.println("\n\n with two separate streams \n");
i=5;
while (--i>0) {
    System.err.println("Number must be possitive!");
    System.out.println("New try:");
}

Output:

Number must be possitive!
New try:
Number must be possitive!
New try:
Number must be possitive!
New try:
Number must be possitive!
New try:


 with two separate streams 

Number must be possitive!
New try:
New try:
Number must be possitive!
Number must be possitive!
New try:
Number must be possitive!
New try:

As both streams are not in synch the order in which they write is not guaranteed.

the Hutt
  • 16,980
  • 2
  • 14
  • 44
-1

I would suggest checking your IDE settings when it comes to console. It happens to be running just fine for me:

Example of mine compilation

The code that I used is basically same:

    import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        
        System.out.print("Enter the number:");
        Scanner scanner = new Scanner(System.in);
    while (true) {
        int num = scanner.nextInt();
        if (num > 0) {
            System.out.println("All good, bye");
            break;
            
        } else {
            System.err.println("Number must be possitive!");
            System.out.print("New try:");
            
        }
        
    }
    }
}
Aldin
  • 85
  • 12