# Help finding sum of an array

• 04-19-2012, 05:19 PM
HalfAZN
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)

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 numElements=Keyboard.readInt();         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));                                 a[count]=Keyboard.readInt();                         }                 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");                 int choice=Keyboard.readInt();                 if(choice==1)                 {                                                         }                 if(choice==2)                 {                 }```

Edit: Program isn't done. I'm just having trouble with the first choice
• 04-19-2012, 05:31 PM
Wnt2bsleepin
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?
• 04-19-2012, 05:34 PM
HalfAZN
Re: Help finding sum of an array
Quote:

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?
• 04-19-2012, 05:37 PM
Wnt2bsleepin
Re: Help finding sum of an array
Quote:

Originally Posted by HalfAZN
How do I do this?

You can get the length of the array like this:

Code:

`arrayname.length;`
Notice there is no () there.
• 04-19-2012, 05:50 PM
HalfAZN
Re: Help finding sum of an array
Quote:

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

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

Code:

```if(choice==1)                 {                                 System.out.println("Sum: " +a[k]);         }```
Attachment 3598
• 04-19-2012, 08:33 PM
Wnt2bsleepin
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.
• 04-20-2012, 12:08 AM
HalfAZN
Re: Help finding sum of an array
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 numElements=Keyboard.readInt();         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));                                 a[count]=Keyboard.readInt();                         }                 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");                 int choice=Keyboard.readInt();                 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
• 04-20-2012, 03:00 AM
Wnt2bsleepin
Re: Help finding sum of an array
If you want to sum up the elements in the array, you should use a loop.
• 04-20-2012, 06:23 AM
HalfAZN
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
• 04-20-2012, 09:06 AM
Sierra
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
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.
• 04-20-2012, 05:21 PM
HalfAZN
Re: Help finding sum of an array
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
• 04-20-2012, 05:51 PM
Sierra
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?
• 04-23-2012, 05:09 PM
HalfAZN
Re: Help finding sum of an array
Quote:

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?

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);
• 04-23-2012, 05:18 PM
Wnt2bsleepin
Re: Help finding sum of an array
Quote:

Originally Posted by HalfAZN
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];