Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Whats wrong with system.err?

 
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
 
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.
 
Author
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.
 
Saloon Keeper
Posts: 22011
151
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic