# Thread: Help finding sum of an array

1. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Help finding sum of an array

I have to make a program where I have the user enter the amount of numbers they want to input, and then have a menu that allows them to see the sum, average, printed order, reverse order, number of even numbers, and number of odd numbers. To find the sum of the numbers, I have to know how many numbers the user has entered, but I'm not sure how. (It's choice 1 in the program)

Java Code:
```import cs1.Keyboard;
import java.util.*;
public class moreArray
{
public static void main(String[] args)
{

System.out.println("How many elements would you like to enter");
int[] a = new int[numElements];
int count;
int[]b=new int[numElements];

if(numElements<=0)
System.out.println("Invalid number. Must be greater than 0");

else
{

for (count=0; count<numElements; count++)
{
System.out.println("Please enter a number for bucket " +(count+1));
}

System.out.println("1. Sum of numbers");
System.out.println("2. Average of numbers");
System.out.println("3. Print list backwards");
System.out.println("4. Print list forwards");
System.out.println("5. Count even numbers");
System.out.println("6. Count odd numbers");
System.out.println("7. Quit");

if(choice==1)
{

}

if(choice==2)
{

}```

Edit: Program isn't done. I'm just having trouble with the first choice
Last edited by HalfAZN; 04-19-2012 at 05:24 PM.

2. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
5

## Re: Help finding sum of an array

Sounds like you are putting these numbers into an array. Why not use the length of the array to determine how many numbers the user entered?

3. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Originally Posted by Wnt2bsleepin
Why not use the length of the array to determine how many numbers the user entered?
How do I do this?

4. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
5

## Re: Help finding sum of an array

Originally Posted by HalfAZN
How do I do this?
You can get the length of the array like this:

Java Code:
`arrayname.length;`
Notice there is no () there.

5. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Originally Posted by Wnt2bsleepin
You can get the length of the array like this:

Java Code:
`arrayname.length;`
Notice there is no () there.
I sent an int (k) equal to a.length, and everything compiled, but when I ran it

Java Code:
```if(choice==1)
{

System.out.println("Sum: " +a[k]);
}```

6. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
5

## Re: Help finding sum of an array

Can you post the rest of the code with the correction you added? You seem to be trying to print out an array that doesn't exits. If you wanted to print out the last element in the array, use k-1.

7. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Java Code:
```import cs1.Keyboard;
import java.util.*;
public class moreArray
{
public static void main(String[] args)
{

System.out.println("How many elements would you like to enter");
int[] a = new int[numElements];
int count;
int[]b=new int[numElements];
boolean go;
int k=a.length;

int counter;

if(numElements<=0)
System.out.println("Invalid number. Must be greater than 0");

else
{

for (count=0; count<numElements; count++)
{
System.out.println("Please enter a number for bucket " +(count+1));
}

System.out.println("1. Sum of numbers");
System.out.println("2. Average of numbers");
System.out.println("3. Print list forwards");
System.out.println("4. Print list backwards");
System.out.println("5. Count even numbers");
System.out.println("6. Count odd numbers");
System.out.println("7. Quit");

if(choice==1)
{

System.out.println("Sum: " +a[k]);
}

if(choice==2)
{

}

if(choice==3)
{

}

}

}

}```

I realize that a[k] isn't the sum, but not sure where to go
Last edited by HalfAZN; 04-20-2012 at 12:13 AM.

8. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
5

## Re: Help finding sum of an array

If you want to sum up the elements in the array, you should use a loop.

9. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

What would that look like? I'm assuming it would be a for loop, but I"m not sure how to construct it

10. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: Help finding sum of an array

Try to use the for loop, you should come back with the new code if you have trouble - it will look similar to
Java Code:
`for(int i=0; i<myarray.length; i++) iResult += myarray[i];`
I would also suggest you read about using the switch() statement for your choice evaluation at the end of the program.
Last edited by Sierra; 04-20-2012 at 09:09 AM.

11. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Java Code:
```if(choice==1)
{

for( count=0; count<a.length; count++)
{
countResult += a[count];
}
System.out.println("Sum: " +a[k]);
}```
Is this what it should look like? It gets an error, so not sure

12. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: Help finding sum of an array

It would be helpful if you tell us WHAT error... ;)
I would say the sum is countResult, not a[k], right?

13. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Originally Posted by Sierra
It would be helpful if you tell us WHAT error... ;)
I would say the sum is countResult, not a[k], right?
Java Code:
```if(choice==1)
{

for( count=0; count<a.length; count++)
{
countResult += a[count];
}
System.out.println("Sum: " +a[k]);
}```

Errors:

symbol : variable countResult
countResult += a[count];

symbol : variable countResult
System.out.println("Sum: " +countResult);

14. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
5

## Re: Help finding sum of an array

Originally Posted by HalfAZN
Java Code:
```if(choice==1)
{

for( count=0; count<a.length; count++)
{
countResult += a[count];
}
System.out.println("Sum: " +a[k]);
}```

Errors:

symbol : variable countResult
countResult += a[count];

symbol : variable countResult
System.out.println("Sum: " +countResult);
Where did you define CountResult?

15. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: Help finding sum of an array

Probably because you did not declare that variable... and if you are on it, remember to initialize it with zero before you enter the loop. :)

16. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help finding sum of an array

Thanks to both of you. That turned out to be the problem. Didn't define CountResult ._.

#### Posting Permissions

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