# Thread: Getting the sum of the numbers in an array. Help Please!

1. ## Re: Getting the sum of the numbers in an array. Help Please!

Where did you learn to count? I can see four of those method calls in your latest code ...

kind regards,

Jos

2. Member
Join Date
Oct 2011
Posts
18
Rep Power
0

## Re: Getting the sum of the numbers in an array. Help Please!

thats embarrassing :/ haha well i took out one of those and it takes 3 inputs now, but only adds the last 2.

3. Member
Join Date
Oct 2011
Posts
18
Rep Power
0

## Re: Getting the sum of the numbers in an array. Help Please!

I got it to work. Here's the code that made it work.
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{

Scanner sj = new Scanner(System.in);

int[] numbers1 = new int[3];
numbers1[0] = sj.nextInt();
numbers1[1] = sj.nextInt();
numbers1[2] = sj.nextInt();

int sum = 0;

for(int i = 0; i < numbers1.length; i++)
{
sum += numbers1[i];

}

System.out.println("Here's the sum " + sum);

}

}```

4. ## Re: Getting the sum of the numbers in an array. Help Please!

Originally Posted by mike_
I got it to work. Here's the code that made it work....
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{

Scanner sj = new Scanner(System.in);

int[] numbers1 = new int[3];
numbers1[0] = sj.nextInt();
numbers1[1] = sj.nextInt();
numbers1[2] = sj.nextInt();

int sum = 0;

for(int i = 0; i < numbers1.length; i++)
{
sum += numbers1[i];

}

System.out.println("Here's the sum " + sum);

}

}```
Good deal, but I'd still get the user input inside of a for loop. This part here:

Java Code:
```        int[] numbers1 = new int[3];
numbers1[0] = sj.nextInt();
numbers1[1] = sj.nextInt();
numbers1[2] = sj.nextInt();```

5. Member
Join Date
Oct 2011
Posts
18
Rep Power
0

## Re: Getting the sum of the numbers in an array. Help Please!

Originally Posted by Fubarable
Good deal, but I'd still get the user input inside of a for loop. This part here:

Java Code:
```        int[] numbers1 = new int[3];
numbers1[0] = sj.nextInt();
numbers1[1] = sj.nextInt();
numbers1[2] = sj.nextInt();```
How would that work out?

6. ## Re: Getting the sum of the numbers in an array. Help Please!

Originally Posted by mike_
How would that work out?
That is left as an exercise for the reader. :)

Page 2 of 2 First 12

#### Posting Permissions

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