• 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.*

 
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
All the time when I try to print something to console and use different functions System.err.*/.out.* (all: printf, print and println), they are on wrong position.
When I use the same function, there is no problem. So how can I use both err.* and out.* together?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try flushing the stream after you've written something to it, but I don't think it's a guarantee that it will immediately appear in your output window, that behavior is platform dependent.

I'm not sure why it's a problem though. If you want your data to appear in the same output location, you really should be using the same output method.
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have normal execution of the program, and it should be black text. But one example present a case with wrong input, so I want to warn the user with red text. And I want that this red text appear when I start this wrong case example, but not above of all other examples, which are correct.
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:... I'm not sure why it's a problem though. If you want your data to appear in the same output location, you really should be using the same output method.

Mike Savvy wrote:... But one example present a case with wrong input, so I want to warn the user with red text. And I want that this red text appear when I start this wrong case example


Why not just prefix the message with something that indicates it is an warning/error message?
   WARNUNG: Angabe Fehlerhaft
   ERROR: Angabe Fehlerhaft
 
Ranch Hand
Posts: 127
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all: Please post console outputs as TEXT instead of screenshots. It's hard to try to identify what's on some small pictures full of code and log output on a mobile device with a typical screen size somewhere between 4"-6". When posting text the browser rescales it automatically so it can be read as the rest of the post. There is no reason to make a picture if you can just copy'n'paste output text. Please think about that the next time. Thanks.

Back to topic: System.out and System.err are two different output streams, and, more important: independable from each other. They also have special purposes when they should be used. There is no guarantee in which order they will end up if you mix them. If you only have ONE output console it's better you just stick to ONE output stream, preferable System.out and leave System.err alone. If you have TWO distinct output consoles, one for System.out and the other for System.err, you sure can make use out of both of them.
If you really do have to use both output streams into on single console you just have to deal with they may appear in any order. There's just no way from inside Java to sync them. Also: System.err should never be synced to anything but should always handled as fast as possible. You may can delay output on System.out - but please do not do it on System.err - there's a reason why it exists and why it has this name. In addition: IF you really would want to sync these two streams it had to be done in the console receiving them, as to handle two streams it requires two separate threads (or even processes - depends on the overall system architecture) - this can't be controlled by your code.

About colored text: Aside from that a console are only supposed to handle text as text, it depends on if the output console even supports meta-controlling. Sure, modern consoles, shells and terminals often do support such manipulation (on Linux often nCurses is used, cmd and powerShell may support their own stuff) but there's no guarantee for it. In the best case your meta-controlling just doesn't work at all and the user ends up with a shell in its normal color scheme, whatever that might be. In the worst case you send the wrong meta-controll to the wrong shell as the users uses something different than you expect which can end up on severe issues. Also: By fixing on "red text" you assume that the text-color isn't already red and that the background isn't red neither. Although useless, but your "red" text would just vanish when a user has set his console to display red text on red background because some other code run in it was designed for that setting and manipulates it in other ways. Don't assume that the whole world matches your dev machine. So, if you want a special color scheme for your application - don't even bother with low level console but go GUI right away, this solves several of your issues at once: You don't have to worry about order of out and err as you can just use two text boxes, you can set your own color scheme, you're de-couple yourself from some environment specific issues (altough you may can run into new ones if you design your GUI for 4k and a user only as 1280x720 and you don't implement scaling correctly) ...
What's the saying? There're many different ways to get to Rome? Well sure, but there're some very hard and long ones, and maybe even some dead ends (for some specific reasons). I would recommend aiming for the short and easy ones.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and IDEs can accentuate the difference in output speed between System.err and System.out, which you might not notice on a console/terminal window. I am a bit surprised not to see the input via System.in on your screenshot.
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Winter wrote:First of all: Please post console outputs as TEXT instead of screenshots. It's hard to try to identify what's on some small pictures full of code and log output on a mobile device with a typical screen size somewhere between 4"-6". When posting text the browser rescales it automatically so it can be read as the rest of the post. There is no reason to make a picture if you can just copy'n'paste output text. Please think about that the next time. Thanks.


Why so much negativity? If you cant see it, dont see. I have my reason why I do so - it is part of my homework, and I want that nobody will find it when check my homework solutions.
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...and IDEs can accentuate the difference in output speed between System.err and System.out, which you might not notice on a console/terminal window. I am a bit surprised not to see the input via System.in on your screenshot.


Hello Campbell! Yes, there is no input because I have a test class. First time I used all outputs with System.out.println, but because of this issue I changed all to System.out.printf, you have recommend me this earlier. But it has not changed the situation.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What difference will it make using printf() rather than println()? The difference is between System.out and System.err. They run on different threads, they may therefore sometimes print their output in the “'wrong” sequence.
 
Ron McLeod
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really need colourized console output, you could try something like Java Colored Debug Printer (JCDP).


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However things like what Ron just pointed out are uncommon and obscure because people haven't been writing serious console-based applications for about 20 years now.
 
Bob Winter
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Savvy wrote:Why so much negativity? If you cant see it, dont see. I have my reason why I do so - it is part of my homework, and I want that nobody will find it when check my homework solutions.


Well, if I'm correct there's a recommendation on this topic to do as I pointed out: To prefer post text output as text instead of screenshots. If you want to argue about it please contact staff.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Winter wrote:. . . a recommendation on this topic to do as I pointed out: . . .

. . . to make sure that Bob Winter can read the whole of the posts.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:However things like what Ron just pointed out are uncommon and obscure because people haven't been writing serious console-based applications for about 20 years now.



Snicker.

My production servers have no windowing systems running. Outside of apps running in service containers (like JEE), all my applications are console/shell apps.

Note for Campbell: System.out and System.err do not run under dedicated threads. They run under the threads that do the actual System io methods. Which means that you can scramble stuff within System.out or System.err if more that one thread is using them. But even in a single-threaded environment, each System stream has its own distinct buffers and those buffers will not be output to their respective io streams until the properly trigger is encountered - an end-of-line for text output, a full buffer, or an explicit buffer flush() operation. Note that in some cases, close() does not flush() and you can lose stuff, so it's very good practice to explicitly flush before close unless the docs for your selected IO class explicitly promise to do so for you.

Also, note for everybody: internal to the Java app and JVM, System.out and System.err are two entirely independent output streams. However, a command-line app's shell will deal with each one as it has been ordered to. For example, by using redirection. By default, the two streams just dump straight to shell output with no attempt at co-ordination.

Finally, while the System output streams are appropriate for user interaction, logging should be done using one of the Java log subsystems. They are much better suited.

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Savvy wrote:

Bob Winter wrote:First of all: Please post console outputs as TEXT instead of screenshots. It's hard to try to identify what's on some small pictures full of code and log output on a mobile device with a typical screen size somewhere between 4"-6". When posting text the browser rescales it automatically so it can be read as the rest of the post. There is no reason to make a picture if you can just copy'n'paste output text. Please think about that the next time. Thanks.


Why so much negativity? If you cant see it, dont see.


I believe this violates our BeNice policy (that's a link).  Also, Bob is speaking for all mobile users, not just him.

I have my reason why I do so - it is part of my homework, and I want that nobody will find it when check my homework solutions.


I can understand that.  However, it's a policy that you copy and paste code, not attach a screenshot.  See PostTextNotScreenshots (that's also a link).
 
Bob Winter
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Bob Winter wrote:. . . a recommendation on this topic to do as I pointed out: . . .

. . . to make sure that Bob Winter can read the whole of the posts.


although that's kind of sarcastic it's not just me and not just on this very site - am I right this here is still a programmers forum? so why not just post code in its clean form: as text - instead of making screenshots from them (which, although not often, but still sometimes, can end up in people using their phone to take a picture from the screen - which end up really bad)
also: it's most likely not me the only one complain about such - I guess there are at least SOME users using mobile devices with rather small screen sizes like 4" - 6" - and depend on the resolution of the screenshot and can get hard to next impossible to zoom on an image - and yes, although it may weren't the nicest words I think I made my point clear - and if I look at what Knute Snortum posted I'm not wrong about it
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not just mobile devices. I've had screen shots render as generic icons even on the desktop. Not that a fuzzy image resulting from scaling is much better.

I always encourage people to copy-and-paste actual text wherever possible.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have enough difficulty reading screenshots on a 10.6″ screein.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Winter wrote:so why not just post code in its clean form: as text - instead of making screenshots from them (which, although not often, but still sometimes, can end up in people using their phone to take a picture from the screen - which end up really bad)



Apparently there are a lot of people these days for whom sending images back and forth is a serious form of communication. And there are plenty of billionaire-run web apps which facilitate that form of communication.

And I see that people are willing to watch people typing in Youtube videos for the purpose of learning about programming. So again, images are better than text for those people.

But yeah, I agree that programming is essentially a text-based activity. Much of what I learned about Java has been from reading text-based tutorials, followed by downloading their (text-based) example code and modifying it to do other things. Text is so much better than images for those things -- if I'm watching a Youtube video, how do I skip forward to the part where it starts talking about some particular subtopic? I just have to wait for it, or skip back and forth until I stumble over it.

But this is the Beginning Java forum. Beginners don't know what they don't know, so they just start with what they do know. If "images better than text" is what they do know, and "copy and paste" is what they don't know, then you're going to get screenshots. Not much point complaining about it.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Savvy wrote: If you cant see it, dont [sic] see. I have my reason why I do so


well...one could use that same argument and say that if you don't like this site's policies, don't use it.  This site has its reasons why it does what it does.  ;-)

people here are EXTREMELY helpful.  Nobody here gets paid.  Everyone is volunteering their time to help complete strangers.  

My personal philosophy is that if I am going to ask someone to give up their time to help me, for free, i'm going to do EVERYTHING I can to make it as EASY as possible for them to help me.  And they get to decide what is easy for them.

You (probably) won't be banned for violating that policy, but i do guarantee fewer people will be willing to help you.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't mean to be sarcastic in my last post on this th‍read, but I obviously came across like that. Sorry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic