5

This isn't a major issue, but I don't understand why this happens, so I figured I'd post it here. This is my code:

do{
    printMenu();//method to print menu
    try{
        user=input.nextInt();
    }
    catch(InputMismatchException imme)
    {
        System.err.println("Make sure to enter a number.");
        input.next();
        continue;
    }
    switchMenu(user);//method with switch method for user input
}while(1<2);

The code runs fine except for one thing. The error message Make sure to enter a number. sometimes displays after the menu, sometimes before, sometimes in the middle of the menu. This is the output of the program:

1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit

a

1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit

Make sure to enter a number.//Error message after menu

asd//wrong input

Make sure to enter a number.//now error message displays before menu

1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit

asd

1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
Make sure to enter a number.//in the middle now???
4. Print seat map
5. Check price
6. Print ticket
7. Exit

I am using Eclipse if it matters. I know it's not a big problem, but I'm curious why this happens.

Yulek
  • 331
  • 3
  • 12
  • As suggested, I changed System.err to System.out and it works just fine. Appreciate the help. – Yulek Apr 25 '15 at 00:23

2 Answers2

3

This is due to the fact that System.out and System.err are two different streams which might buffer write operations and get flushed (i.e. printed out) at different times.

Things probably get mixed up between System.out and System.err, because I'd assume System.out to be implemented using a buffered output stream for performance reasons and System.err to be implemented using a non-buffered stream to get error messages printed more quickly.

You will probably observe a behaviour closer to what you expect when you explicitly call flush() on the streams, although I'm not sure if there won't be race conditions left, as both will end up in the same Eclipse console.

That being said, I'd also assume that the Eclipse console has means of mixing up strings (i.e. even if your code guarantees an order, the console might give System.err a higher priority), so the only reliable way of getting a guaranteed order would IMHO be using the same output stream for both messages.

Marvin
  • 13,325
  • 3
  • 51
  • 57
1

System.err and System.out are different PrintStream and are both asynchronous. There is no way to predict which one will be display after the other (if you call them both quickly).

If you really want your messages to be displayed in order, you should use the same PrintStream (System.err, System.out or your own PrintStream).

grebesche
  • 511
  • 1
  • 3
  • 14
  • Makes sense, except why did it print in the middle of my menu that one time, when the whole menu is in a different method? – Yulek Apr 25 '15 at 00:16
  • Because it may append that both `PrintStream` are writing to the console at the same time. – grebesche Apr 25 '15 at 00:19