View Full Version : Program won't execute methods sequentially (consistently)!

03-02-2012, 12:30 AM

OK, I'm very new to Java (and OOP) and just trying to get a grip of it all. So far so good but just hit this little problem. I myself can't find any errors in the code, and Netbeans compiles it fine without producing any errors.
Basically, my intentions are to output the invalid args[] errors (if any) followed by the two equations.
Each time I run it, the output is listed randomly, sometimes correctly, sometimes not (like below).
I've re-positioned everything countless times but it's still always random!
Please help!

Illegal input at argument 2 with input a. Characters only!
The sum of valid passed arguments is 3
Illegal input at argument 4 with input b. Characters only!
The average of the passed arguments is 2.0
BUILD SUCCESSFUL (total time: 0 seconds)

class Averager {

public static void main(String[] args) {

int sum = 0;
int validArgs = 0;

if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (notLegalCharacters(args[i])) {
System.err.print("Illegal input at argument " + (i + 1) + " with input " + args[i] + ". Characters only! \n");
else {
sum += Integer.parseInt(args[i]);
System.out.println("The sum of valid passed arguments is " + validArgs);
System.out.println("The average of the passed arguments is " + (float) sum / validArgs);
else {
System.err.println("You did not provide any arguments!");

static boolean notLegalCharacters(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)))
return true;
return false;

03-06-2012, 10:37 PM
Try changing your System.err.print to System.out.println. It might be something as simple as using different writers is allowing for some weird thread condition.

03-06-2012, 11:20 PM
it is all in where rthe stream is writing- I am assuming that behind the scenes the system.out self closes the stream
ie stream.close()
where as the .err may not do so without some delay because it is checking to see if there is anywhere else it should write the message too... see this forum's bable about it...
link (http://www.coderanch.com/t/379903/java/java/diff-between-System-out-System)...
there is some bickering there about who is right and who is not- i tried to find some definitive proc infor for you but the bottom line is keep it consistent... I would use the .err to write to a log method (however you handle error logging) and use system.out to write to the console

Edit: more elaboration of how I *think it might be working...

ok what do you want em to write? "blah"
systemstream "blah"

ok what do you want me to write? "blah"
systemstream "blah"
you want me to write that anywhere else? no
ok systemstream.close

*just my thoughts on the matter though

Edit edit:
I am assuming here that there is some delegate in the .err method that handles the command argument and it needs to spit back the process of that argument even if that argument is null

03-07-2012, 12:51 AM
Thank you both for your help, this clears so much up for me.
Interesting how this could happen, a real eye opener.

Such a simple program, yet I must have restructured it about 10 times wondering why the hell it wasn't working. I swore it was logical.

Thanks again.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum