Are you working with array types: i.e. int
or the Collections framework with java.util.List: i.e. List<Integer> ?
The word "add" has multiple meanings.
In mathematics add means to sum numbers together.
In terms of a list, add means "put on the end of the list"
So the "add" method you called on your numsList probably won't do what you want it to.
I would assume that you are dealing with an array.
In that case you probably want to
- set up a variable "total" to store the result
- loop through the array, adding each element from the array to "total"
Daniel Grinevich wrote:I need help with the code below. I'm working with Arraylists and I cant figure out how to add up all the numbers within an arraylist except for numbers like 10 and over.
forget about java...how would YOU do it? Tell me in English what you would do if you were given a bunch of numbers and had to complete the task.
If my assumption is correct then your question already has the answer. You'll just write another for loop to iterate over the list and in that loop will do something like sum += a[i], where sum is an int you defined before the loop with the value 0. So once the loop finishes, the sum variable will contain the sum of all the numbers.
Hope this helps.
Earilier today, I wrote:. . . Challenge: Now we have had Java8 for nearly two years, work out the average without using the keywords do for and while.
There are lots of other possible versions of that solution. I shall be very naughty and leave Winston to explain how that code works
java ListAverager 273 872359 76324 -83745 12 0 -34875