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):
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):
I can't figure out how it is making these weird prints. Can someone tell me what am I missing?