• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out and System.err switch positions  RSS feed

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following recursive code which returns a y or n.



When I enter incorrect data the error message (line 5) sometimes prints before, sometimes after and sometimes on the same line as the prompt message (line 2 after the recursive call on line 6). If I use System.out instead of System.err it works fine. My understanding is I should use System.err for an error message so can someone explain what is going on here? Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out and System.err are not synchronized with regard to one another, so there are no guarantees about the order in which things are printed between the two, even if they print to the same console window.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend System.out in this case - you want to give feedback to the user based on their input.
System.out should be used for any output your program does as a matter of course.

System.err is more for documenting unexpected errors/exceptions. Stuff you don't expect to happen as part of the normal program.
e.g. Unable to save a file because you ran out of disk space, unable to connect to database etc etc.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!