0

I am trying to print some statements in java using System.out.println() statements but they aren't printing sequentially.

if(us02_birth_b4_marriage())
{
    System.out.println("All user stories passed succesfully");
}
else
{
    System.out.println("There are following errors: ");
    for(String failString: failures)
    {
        System.err.println(failString);
    }
    //System.exit(0);
}

System.out.println("Individual");
System.out.format("%-10s%-20s%-10s%-15s%-10s%-15s%-15s%-20s%-20s\n", "ID", "Name", "Gender", "Birthday", "Age", "Alive", "Death", "Child", "Spouse");

The method is defined at the beginning of the class. Does that make any difference? Please see the output here Output Image

Community
  • 1
  • 1
Kunj
  • 11
  • 5
  • What do you mean sequentially, in this context? If you mean by the order it is written in your code, that is not going to happen unless your first `if` statement is always `true`. – Suede Feb 24 '20 at 01:06
  • By order I mean, It prints "Individual" first, then "There are following errors: ", then System.err.println() and then System.out.format() statement. And this sequence keeps on changing everytime. Ideally, it should print, "There are following errors", System.err.println(), "Individual", System.out.format(). – Kunj Feb 24 '20 at 01:18
  • 2
    You will need to write a [mcve]. – kaya3 Feb 24 '20 at 01:18
  • 3
    Does this answer your question? [Random printing order for System.out & System.err calls](https://stackoverflow.com/questions/12594537/random-printing-order-for-system-out-system-err-calls) and [System.out.println and System.err.println out of order](https://stackoverflow.com/q/1883321/12299000) – kaya3 Feb 24 '20 at 01:31
  • you could try to force flush for System.out and System.err after each println, however, I believe it's already auto flushing after each new line character. But might be something to try – LZR Feb 24 '20 at 01:42
  • @pavel.lazar I tried putting flush after every statement but it still gave me the same bug. What I found out is that there is no synchronization available and its a long-lasting bug in eclipse. However, trying in another IDE may help. – Kunj Feb 24 '20 at 01:47
  • Well yes, it might. But it won't fix the problem in general. The real answer is to adjust your expectations. Don't rely on any specific ordering of console output when you write to both `System.out` *and* `System.err`. FWIW, it is not really an Eclipse bug. – Stephen C Feb 24 '20 at 05:48
  • @StephenC Please check - https://bugs.eclipse.org/bugs/show_bug.cgi?id=32205. It says "This has been improved since 2.0, but since writing/reading from out/err streams is non-deterministic (i.e. we have separate threads reading output from each stream), the result can be non- deterministic." – Kunj Feb 24 '20 at 06:35
  • I already looked at it. It is marked as FIXED. Meaning that any residual strange behavior is not regarded as a bug anymore. (Or ... if you are using an old version of Eclipse, upgrade to 4.13 == 2019-09). – Stephen C Feb 24 '20 at 06:54
  • Either way, the Java specs provide no guarantees on the interleaving of output to different streams redirected to the same place by the external environment. So the old behavior of Eclipse is not buggy from the perspective of the Java specs. (This is probably why it took such a long tome for the developers to get around to improving the behavior.) – Stephen C Feb 24 '20 at 07:01
  • @StephenC Alright! I get it now, I guess. Thank you for your help! – Kunj Feb 24 '20 at 07:01

1 Answers1

0

The problem is that you are writing some messages to System.out and others to System.err. This may result in the lines being in an unexpected order (depending on what you expect ...). This is primarily because System.out and System.err are different streams with different (Java side) buffering characteristics.

Given the way you are running your application, they happen to write to the same place; i.e. your console. But even so, the OS does not guarantee that the interleaving of standard output and standard error lines will correspond exactly to the order that the application wrote them.

Solution: if you want a guarantee that that the lines will appear on the console in the order that your code writes them, you must write them to the same stream.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216