Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-01-2012, 11:30 PM   PM User | #1
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
Program won't execute methods sequentially (consistently)!

Hey.

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!


run:
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)


Code:
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]);
                    validArgs++;
                }
            } 
            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;
    }
}

Last edited by dan-dan; 03-02-2012 at 11:53 AM.. Reason: Made a couple of changes but still the same problem
dan-dan is offline   Reply With Quote
Old 03-06-2012, 09:37 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Users who have thanked Aradon for this post:
dan-dan (03-06-2012)
Old 03-06-2012, 10:20 PM   PM User | #3
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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...
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...

system.out
ok what do you want em to write? "blah"
systemstream "blah"
systemstream.close

system.err
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
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE

Last edited by alykins; 03-06-2012 at 10:44 PM..
alykins is offline   Reply With Quote
Users who have thanked alykins for this post:
dan-dan (03-06-2012)
Old 03-06-2012, 11:51 PM   PM User | #4
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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.
dan-dan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 AM.


Advertisement
Log in to turn off these ads.