0

I am a beginner in programming. As a part of my practice, I created a simple game, and I am having some problem in using out and err. The problem is described below with a portion of code and a screenshot of my output:

 do{
                System.out.print("Press 'r' to play, 'i' to get info and 'q' to quit: ");
                input = scan.next().charAt(0);
                switch (input){
                    case 'r':
                        roundCount++;
                        System.out.print("A random number is generated for you, guess what it is: ");
                        int answer = scan.nextInt();
                        allAnswers.add(answer);
                        randomNumber = getRandom();
                        if(isRightAnswer(answer,randomNumber)){
                            System.out.print("Congrats! that was absolutely right guess, the number was "+randomNumber + ", ");
                            scoreCount += 5;
                            rightAnswers.add(answer);
                        }else{
                            System.out.print("Oops!, wrong guess, right answer is "+randomNumber +", ");
                            wrongAnswers.add(answer);
                        }
                        break;
                    case 'q':
                        System.out.println("Bye Bye!");
                        break;
                    case 'i':
                        printInfo();
                        break;
                    default:
                        System.out.print("Wrong Input --> ");
                        break;
                }
            }while(input != 'q');

The problem is that both out and err behaves differently, as shown in the following two images, one output is generated by out and the other by err:

Generated by System.out System.out will print the line "Wrong input! ->" before the line "Press 'r' to play, 'i' to get info and 'q' to quit:", which is exactly what I want

While System.err will print the line "Wrong input! ->" after the line "Press 'r' to play, as shown below:" Generated by System.err

What is the real reason behind this variety in the behavior of these two streams??

HusenPatel
  • 49
  • 4
  • 2
    has it occurred to you that if they behaved the same, there wouldn't be any need for two methods? – Stultuske Jan 31 '22 at 07:10
  • So I want to know the reason behind this. Because I want to use err instead of out for showing an error and get the desired output – HusenPatel Jan 31 '22 at 07:17
  • Why do you want to use `err` when you are getting the desired output using `out`? – tgdavies Jan 31 '22 at 07:50
  • 1
    [Read this SO Thread](https://stackoverflow.com/questions/1883321/system-out-println-and-system-err-println-out-of-order) – DevilsHnd Jan 31 '22 at 08:07

2 Answers2

1

It looks like you use intelliJ witch seperates the err stream more then the normal command line output. It will color it red and do other debugging stuff. It can even happen that out lines executed after an err line will show up first. Only use the error stream for critical program malfunction not for wrong user actions. EDIT: even on the normal command line its not guaranteed that the two streams will show up in order, since you create a race condition about the command line window

0

Probably depend on the IDE you are using, I ran your code in my eclipse IDE using System.out and System.err and got the same outout for both. Probably in yours the out and err streams are being flushed differently.

shallow
  • 1
  • 3