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
:
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:"
What is the real reason behind this variety in the behavior of these two streams??