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

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

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

Here is the code. I want to get the sum of the 3 numbers the user inputted. All I am getting is numbers that make no sense.

import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int numbers;
Scanner sj = new Scanner(System.in);
numbers = sj.nextInt();

String[] numbers1 = new String[3];
numbers1[0] = sj.nextLine();
numbers1[1] = sj.nextLine();
numbers1[2] = sj.nextLine();

for(numbers = 0; numbers < 3; numbers += 3)
{
System.out.println("Your three numbers added togeter: " + numbers1[numbers] + numbers1[numbers + 2]
+ numbers1[numbers + 2]);

}

}

}

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

You're using a String[] array, and thus are adding (concatenating) Strings, so it's understandable that this code won't work. Instead you'll need to convert the Strings to int, either by reading them into an int array using your Scanner, or by parsing the Strings into ints using Integer.parseInt(...). Be carefully how you use the Scanner as some of your uses of it above don't make sense (calling nextInt once and nextLine 3 times?).

3. 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
You're using a String[] array, and thus are adding (concatenating) Strings, so it's understandable that this code won't work. Instead you'll need to convert the Strings to int, either by reading them into an int array using your Scanner, or by parsing the Strings into ints using Integer.parseInt(...). Be carefully how you use the Scanner as some of your uses of it above don't make sense (calling nextInt once and nextLine 3 times?).

How would I be able to convert the strings to ints and put them in an array? I'm sorry I'm new with arrays, just learning them.

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

You could create an int array to begin with, and Scanner already has a method that allows you to read text as ints, and in fact you've already tried to use this method -- Scanner#nextInt(String s). You'll want to experiment with it some more. :)

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
You could create an int array to begin with, and Scanner already has a method that allows you to read text as ints, and in fact you've already tried to use this method -- Scanner#nextInt(String s). You'll want to experiment with it some more. :)
I'm still a little confused :/ would you be able to give me code example or a hint at one?

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

Let me outline the steps that I think you'll need:

1. Create an int array of appropriate size, say called numbers
2. Create your Scanner object and initialize it with System.in (as you've already done)
3. Prompt your user to enter 3 numbers (as you've already done)
4. Use a for loop, loop through your int array going from i = 0 to i < numbers.length.
5. Use the correct Scanner method to get the int from the user. It's not nextLine() which returns a String, but instead use nextInt();
6. Assign the result returned from nextInt() into your array. Again, you're still inside of the for loop, so you'll use the loop index, usually "i" as the index to the array.
7. You're done with this for loop, now create a second for loop to add up the numbers held by the array.

Try to create a program based on this. Make sure to compile after each new line of code and to fix compilation errors before adding any new lines of code.

Luck!

7. 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
Let me outline the steps that I think you'll need:

1. Create an int array of appropriate size, say called numbers
2. Create your Scanner object and initialize it with System.in (as you've already done)
3. Prompt your user to enter 3 numbers (as you've already done)
4. Use a for loop, loop through your int array going from i = 0 to i < numbers.length.
5. Use the correct Scanner method to get the int from the user. It's not nextLine() which returns a String, but instead use nextInt();
6. Assign the result returned from nextInt() into your array. Again, you're still inside of the for loop, so you'll use the loop index, usually "i" as the index to the array.
7. You're done with this for loop, now create a second for loop to add up the numbers held by the array.

Try to create a program based on this. Make sure to compile after each new line of code and to fix compilation errors before adding any new lines of code.

Luck!

I honestly appreciate you doing this! It has been a lot of help. Now I have done the following

Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int number;
Scanner sj = new Scanner(System.in);
number = sj.nextInt();

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

for(i = 0; i < number.length)
{

}

}

}```

It is giving me an error in the for loop, and this is obviously because there is nothing in it. But how would I make the second loop just to add the number? Can I just make a println statement to add them up?

Once again I really appreciate it!

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

Originally Posted by mike_
I honestly appreciate you doing this! It has been a lot of help. Now I have done the following...

It is giving me an error in the for loop, and this is obviously because there is nothing in it.
No, an empty for loop will not cause an error. The error is because you've written the for loop wrong, you're missing the code that increments the index variable. Check out your tutorial or text on for loops to see how to do it right. You also should get the variables from the user with a for loop.

9. 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
No, an empty for loop will not cause an error. The error is because you've written the for loop wrong, you're missing the code that increments the index variable. Check out your tutorial or text on for loops to see how to do it right. You also should get the variables from the user with a for loop.
I have got it to almost work. I know haha such a simple thing and I can't seem to grasp it. Heres the code
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int number;
Scanner sj = new Scanner(System.in);
number = sj.nextInt();

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

for(int i = 0; i < numbers1.length; i += 3)
{
int sum = numbers1[i] + numbers1[i +1] + numbers1[i +2];
System.out.println("Here's the sum " + sum);
}

}

}```
It takes numbers and adds them together. The only thing. It takes 4 numbers and adds the last 3 together and they add together right. But for some reason its taking 4?

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

Java Code:
`for(int i = 0; i < numbers1.length; i += 3)`
i += 3 !!!???

No, don't do this. You want to use a regular simple for loop, one that increments the same as 99% of all for loops, via i++.

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

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

oh for some reason, I did another array and I used that there. But I have no put the i++ and this is what I'm getting in the output.

1
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Array2.main(Array2.java:20)
Here's the sum 9

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

Originally Posted by mike_
oh for some reason, I did another array and I used that there. But I have no put the i++ and this is what I'm getting in the output.

1
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Array2.main(Array2.java:20)
Here's the sum 9

13. 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
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int number;
Scanner sj = new Scanner(System.in);
number = sj.nextInt();

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

for(int i = 0; i < numbers1.length; i++)
{
int sum = numbers1[i] + numbers1[i +1] + numbers1[i +2];
System.out.println("Here's the sum " + sum);

}

}

}```

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

Originally Posted by mike_
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int number;
Scanner sj = new Scanner(System.in);
number = sj.nextInt();

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

for(int i = 0; i < numbers1.length; i++)
{
int sum = numbers1[i] + numbers1[i +1] + numbers1[i +2];
System.out.println("Here's the sum " + sum);

}

}

}```
Make an int above the loop and call it sum,
delete the int sum on your loop,
then do sum+= numbers1[i] in your loop. and move the println from your loop to below the loop :)

15. 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 PhQ
Make an int above the loop and call it sum,
delete the int sum on your loop,
then do sum+= numbers1[i] in your loop. and move the println from your loop to below the loop :)
Java Code:
```import java.util.Scanner;
public class Array2
{
public static void main(String[]args)
{
int number;
Scanner sj = new Scanner(System.in);
number = sj.nextInt();

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

int sum;

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

}

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

}

}```
It's giving me the error on the both of the sums.

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

I think you need initialize the sum,
do int sum = 0; instead of just int sum;

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

Yep, and change the body of that for loop to:

Java Code:
`sum+= numbers1[i]`
kind regards,

Jos

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

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

I did both of those things. I''m not getting an error anymore. But it still lets 4 numbers get inputted.

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

How many nextInt() method calls do you see in your code?

kind regards,

Jos

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

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

there's 3 nextInt()

Page 1 of 2 12 Last

#### Posting Permissions

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