Sum of numbers in through command line arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imtiazalikhan
    New Member
    • Jun 2007
    • 3

    Sum of numbers in through command line arguments

    hi i am trying to sum 10 numbers passing them by command line line argument
    but the problem is it only work with 1 and if i passing other numbers then again it will show the result of number1:
    code:
    class arguments
    {
    public static void main(String args[])
    {

    int sum=0;
    int avg=sum/10;

    System.out.prin tln(args[0]);
    System.out.prin tln(args[1]);
    System.out.prin tln(args[2]);
    System.out.prin tln(args[3]);
    System.out.prin tln(args[4]);
    System.out.prin tln(args[5]);
    System.out.prin tln(args[6]);
    System.out.prin tln(args[7]);
    System.out.prin tln(args[8]);
    System.out.prin tln(args[9]);
    System.out.prin tln("the sum of above numbers:");
    for(int i=args.length; i<=10; i++)
    {

    sum+=i;

    System.out.prin tln(sum);
    System.out.prin tln("Average of 10 numbers:");
    System.out.prin tln(avg);


    }

    }
    }
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by imtiazalikhan
    hi i am trying to sum 10 numbers passing them by command line line argument
    but the problem is it only work with 1 and if i passing other numbers then again it will show the result of number1:
    code:
    [code=java]class arguments
    {
    public static void main(String args[])
    {

    int sum=0;
    int avg=sum/10;

    System.out.prin tln(args[0]);
    System.out.prin tln(args[1]);
    System.out.prin tln(args[2]);
    System.out.prin tln(args[3]);
    System.out.prin tln(args[4]);
    System.out.prin tln(args[5]);
    System.out.prin tln(args[6]);
    System.out.prin tln(args[7]);
    System.out.prin tln(args[8]);
    System.out.prin tln(args[9]);
    System.out.prin tln("the sum of above numbers:");
    for(int i=args.length; i<=10; i++)
    {

    sum+=i;

    System.out.prin tln(sum);
    System.out.prin tln("Average of 10 numbers:");
    System.out.prin tln(avg);


    }

    }
    }[/code]
    First of all, please put your code in proper code tags (see the "REPLY GUIDELINES" when you post on the top right corner of the page)

    Now, why don't you post exactly what you see because I don't understand what this means:"again it will show the result of number1"

    Show what happens when you do both so we can understand what's going on...

    Also, avg = 0/10, you should calculate it again after sum is done being computed so it calculates correctly.

    Lastly, shouldn't you put an if statement asking if args.length == 10 or is >= 10 ?

    Good luck,
    -blazed

    P.S. I just looked at your code in the brackets, SOO much easier to look at and I see your problem:

    your for loop translated to psuedocode says the following: from args.length (which is 10) to 10 do whatever is in the loop. So that's why it only posts the first number.

    Change your int value to start at 0 and end at args.length, try and see if that works...

    -blazed

    Comment

    • imtiazalikhan
      New Member
      • Jun 2007
      • 3

      #3
      again it will show the result of number1

      it means that if i pass 1 1 1 1 1 1 1 1 1 1 to command line argument
      it will show the sum of above numbers is 10

      but when i pass 2 5 6 8 4 8 9 3 6 4
      it will give again the result 10.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by imtiazalikhan
        again it will show the result of number1

        it means that if i pass 1 1 1 1 1 1 1 1 1 1 to command line argument
        it will show the sum of above numbers is 10

        but when i pass 2 5 6 8 4 8 9 3 6 4
        it will give again the result 10.
        Look at your loop closely: i starts with value 10 (the number of arguments) and
        the body is executed once, incrementing the sum by 10 and printing it once.

        Your loop header is dead wrong; it should be something like this:

        [code=java]
        for (int i= 0; i < args.length; i++) {
        ...
        }
        [/code]

        Then try again.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by imtiazalikhan
          hi i am trying to sum 10 numbers passing them by command line line argument
          but the problem is it only work with 1 and if i passing other numbers then again it will show the result of number1:
          code:
          class arguments
          {
          public static void main(String args[])
          {

          int sum=0;
          int avg=sum/10;

          System.out.prin tln(args[0]);
          System.out.prin tln(args[1]);
          System.out.prin tln(args[2]);
          System.out.prin tln(args[3]);
          System.out.prin tln(args[4]);
          System.out.prin tln(args[5]);
          System.out.prin tln(args[6]);
          System.out.prin tln(args[7]);
          System.out.prin tln(args[8]);
          System.out.prin tln(args[9]);
          System.out.prin tln("the sum of above numbers:");
          for(int i=args.length; i<=10; i++)
          {

          sum+=i;

          System.out.prin tln(sum);
          System.out.prin tln("Average of 10 numbers:");
          System.out.prin tln(avg);


          }

          }
          }
          You can also use that for statement to display the numbers.

          Comment

          • tmerkaj
            New Member
            • Jun 2007
            • 6

            #6
            Originally posted by r035198x
            You can also use that for statement to display the numbers.
            Code:
             for(int i=0; i<=args.length; i++)
            {
            int j = Integer.parseInt(args[i]);
            sum+=j;
            
            System.out.println(sum);
            System.out.println("Average of 10 numbers:");
            System.out.println(avg);
            
            
            }

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by tmerkaj
              Code:
               for(int i=0; i<=args.length; i++)
              {
              int j = Integer.parseInt(args[i]);
              sum+=j;
              
              System.out.println(sum);
              System.out.println("Average of 10 numbers:");
              System.out.println(avg);
              
              
              }
              Read what I wrote in my reply #4: it should be '<' instead of '<='. I'm 110% sure
              that you dIdn't try to compile and run it.

              kind regards,

              Jos

              Comment

              • praveen2gupta
                New Member
                • May 2007
                • 200

                #8
                Your Code
                Code:
                for(int i=args.length; i<=10; i++)
                {
                
                sum+=i;
                
                System.out.println(sum);
                System.out.println("Average of 10 numbers:");
                System.out.println(avg);
                }
                Ther are two problems in the programs.
                1. you will have to pass 10 argumens at the command prompt and value of
                i will be 10 in that case.

                2. you are adding sum = sum + i , where sum=0 and i=10 so you will get always sum as 10.

                Change your program as follows

                Code:
                for(int i=args.length; i<=10; i++)
                {
                
                sum+= args[i];
                }
                System.out.println(sum);
                System.out.println("Average of 10 numbers:");
                System.out.println(avg);
                
                I Think it will worrk , Try it

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by praveen2gupta
                  Your Code
                  Code:
                  for(int i=args.length; i<=10; i++)
                  {
                  
                  sum+=i;
                  
                  System.out.println(sum);
                  System.out.println("Average of 10 numbers:");
                  System.out.println(avg);
                  }
                  Ther are two problems in the programs.
                  1. you will have to pass 10 argumens at the command prompt and value of
                  i will be 10 in that case.

                  2. you are adding sum = sum + i , where sum=0 and i=10 so you will get always sum as 10.

                  Change your program as follows

                  Code:
                  for(int i=args.length; i<=10; i++)
                  {
                  
                  sum+= args[i];
                  }
                  System.out.println(sum);
                  System.out.println("Average of 10 numbers:");
                  System.out.println(avg);
                  
                  I Think it will worrk , Try it
                  That won't work praveen. Try it and see what it'll do.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Also read (*) my reply #4 again.

                    kind regards,

                    Jos

                    (*) reading: an ancient mental activity that has been lost for the posterity sadly
                    enough; reading enabled people to gain knowledge and understand information.

                    Comment

                    • tmerkaj
                      New Member
                      • Jun 2007
                      • 6

                      #11
                      Originally posted by JosAH
                      Also read (*) my reply #4 again.

                      kind regards,

                      Jos

                      (*) reading: an ancient mental activity that has been lost for the posterity sadly
                      enough; reading enabled people to gain knowledge and understand information.
                      Code:
                      for(int i=args.length; i<=10; i++)
                      i was trying to show that
                      the loop should start from 0 to args.length not from args.length to 10

                      Comment

                      • sandeeptri
                        New Member
                        • Jun 2010
                        • 1

                        #12
                        sol

                        dear its simple, ur loop should be
                        for(i=0;i<args. length;i++)
                        {//body
                        }
                        i m sure it will work ,it can add any number of arguments. for 10 numbers write 10 in place of args.length. i have done it

                        Comment

                        Working...