472,093 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,093 software developers and data experts.

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.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
System.out.println(args[3]);
System.out.println(args[4]);
System.out.println(args[5]);
System.out.println(args[6]);
System.out.println(args[7]);
System.out.println(args[8]);
System.out.println(args[9]);
System.out.println("the sum of above numbers:");
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);


}

}
}
Jun 20 '07 #1
11 16224
blazedaces
284 100+
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:
Expand|Select|Wrap|Line Numbers
  1. class arguments
  2. {
  3. public static void main(String args[])
  4.  {
  5.  
  6.  int sum=0;  
  7.  int avg=sum/10;          
  8.  
  9.    System.out.println(args[0]);
  10.    System.out.println(args[1]);
  11.    System.out.println(args[2]);
  12.    System.out.println(args[3]);
  13.    System.out.println(args[4]);
  14.    System.out.println(args[5]);
  15.    System.out.println(args[6]);
  16.    System.out.println(args[7]);
  17.    System.out.println(args[8]);
  18.    System.out.println(args[9]);
  19.    System.out.println("the sum of above numbers:");
  20.    for(int i=args.length; i<=10; i++)
  21.      {
  22.  
  23.              sum+=i;
  24.  
  25.              System.out.println(sum);
  26.              System.out.println("Average of 10 numbers:");
  27.          System.out.println(avg);
  28.  
  29.  
  30.      }
  31.  
  32.      }
  33.           }
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
Jun 20 '07 #2
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.
Jun 20 '07 #3
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < args.length; i++) {
  2.    ...
  3. }
  4.  
Then try again.

kind regards,

Jos
Jun 20 '07 #4
r035198x
13,262 8TB
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.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
System.out.println(args[3]);
System.out.println(args[4]);
System.out.println(args[5]);
System.out.println(args[6]);
System.out.println(args[7]);
System.out.println(args[8]);
System.out.println(args[9]);
System.out.println("the sum of above numbers:");
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);


}

}
}
You can also use that for statement to display the numbers.
Jun 21 '07 #5
You can also use that for statement to display the numbers.
Expand|Select|Wrap|Line Numbers
  1.  for(int i=0; i<=args.length; i++)
  2. {
  3. int j = Integer.parseInt(args[i]);
  4. sum+=j;
  5.  
  6. System.out.println(sum);
  7. System.out.println("Average of 10 numbers:");
  8. System.out.println(avg);
  9.  
  10.  
  11. }
  12.  
  13.  
Jun 27 '07 #6
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.  for(int i=0; i<=args.length; i++)
  2. {
  3. int j = Integer.parseInt(args[i]);
  4. sum+=j;
  5.  
  6. System.out.println(sum);
  7. System.out.println("Average of 10 numbers:");
  8. System.out.println(avg);
  9.  
  10.  
  11. }
  12.  
  13.  
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
Jun 27 '07 #7
praveen2gupta
201 100+
Your Code
Expand|Select|Wrap|Line Numbers
  1. for(int i=args.length; i<=10; i++)
  2. {
  3.  
  4. sum+=i;
  5.  
  6. System.out.println(sum);
  7. System.out.println("Average of 10 numbers:");
  8. System.out.println(avg);
  9. }
  10.  
  11.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=args.length; i<=10; i++)
  3. {
  4.  
  5. sum+= args[i];
  6. }
  7. System.out.println(sum);
  8. System.out.println("Average of 10 numbers:");
  9. System.out.println(avg);
  10.  
  11. I Think it will worrk , Try it
  12.  
  13.  
  14.  
Jun 28 '07 #8
r035198x
13,262 8TB
Your Code
Expand|Select|Wrap|Line Numbers
  1. for(int i=args.length; i<=10; i++)
  2. {
  3.  
  4. sum+=i;
  5.  
  6. System.out.println(sum);
  7. System.out.println("Average of 10 numbers:");
  8. System.out.println(avg);
  9. }
  10.  
  11.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=args.length; i<=10; i++)
  3. {
  4.  
  5. sum+= args[i];
  6. }
  7. System.out.println(sum);
  8. System.out.println("Average of 10 numbers:");
  9. System.out.println(avg);
  10.  
  11. I Think it will worrk , Try it
  12.  
  13.  
  14.  
That won't work praveen. Try it and see what it'll do.
Jun 28 '07 #9
JosAH
11,448 Expert 8TB
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.
Jun 28 '07 #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.
Expand|Select|Wrap|Line Numbers
  1. for(int i=args.length; i<=10; i++)
  2.  
i was trying to show that
the loop should start from 0 to args.length not from args.length to 10
Jul 18 '07 #11
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
Jun 17 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by Steve Jorgensen | last post: by
13 posts views Thread by placid | last post: by
20 posts views Thread by Casey | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.