• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System.out and System.err switch positions

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic