Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts

    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 12:53 PM. Reason: Made a couple of changes but still the same problem

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • Users who have thanked Aradon for this post:

    dan-dan (03-07-2012)

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,927
    Thanks
    46
    Thanked 203 Times in 202 Posts
    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
    Last edited by alykins; 03-06-2012 at 11:44 PM.

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

  • Users who have thanked alykins for this post:

    dan-dan (03-07-2012)

  • #4
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •