473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 16703
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

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

Similar topics

17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
13
by: placid | last post by:
Hi all, I was just if anyone knows a way to resolve a list of integers say {1,2,3,4,5} into a range that is 1-5? #include <stdlib.h> #include <stdio.h> #include <ctype.h> int main(int...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.