• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats wrong with system.err?

 
Amrish Bharatiya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I wrote the below piece of code to print out a multiplication table on the console and highlight the numbers that are exact square in red.



When I run this code I get a different output every time:

This is what I got on my last run:


If I replace system.err (line #12) with system.out, then the output is fine:



Interestingly when I put a debug point in eclipse and do a step by step debug (with system.err) the out put comes on fine. (I tried flushing the streams for every loop of i and j but it didn't help).

So whats happening when I use system.err? Any pointers appreciated!

Thanks in Advance,
Amrish
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's an issue of System.err being broken. It's combining the use of System.err and System.out.
I added flush() methods and it displayed correctly:I'm sure someone smarter than me can give you the salient details.
 
Amrish Bharatiya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tom, but even after adding the flush after the print I am getting the same (inconsistent) results:



Here is the code I am running:
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You missed two places to add flush(). See my code.
 
Amrish Bharatiya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried copy pasting your exact code, but I am still getting inconsistent results. I guess it might be problem with my compiler.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tip on debugging: Rule #1: Never assume it's someone else's bug.
 
Amrish Bharatiya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that's helpful!!

All I am saying is that it might be something that "I" might have setup my eclipse environment that would be causing it. if you have a solution please do tell.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're running in Eclipse: the output from streams to the output tab is non-deterministic and will be interwoven essentially randomly. You cannot rely on IDE-specific behavior. Run it in a real console.
 
Amrish Bharatiya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:You're running in Eclipse: the output from streams to the output tab is non-deterministic and will be interwoven essentially randomly. You cannot rely on IDE-specific behavior. Run it in a real console.


Thanks David! That did work.
 
Tim Holloway
Saloon Keeper
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be nitpicky:

Use System.out for normal program output and System.err for error output. Most OS's are geared for this usage and it's a little easier if you follow the conventions.

Of course, System,anything should only be used where appropriate - which is primarily in command-line applications. For environments that don't run directly in a command-line shell such as Applets, webapps, mobile apps, and so forth, you should use some sort of logging system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic