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

Printable View

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-21-2012, 09:31 PM
mike_
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);
System.out.println("Please input your three numbers");
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]);

}

}

}
• 01-22-2012, 01:40 AM
Fubarable
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?).
• 01-22-2012, 05:22 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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.
• 01-22-2012, 05:27 PM
Fubarable
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. :)
• 01-22-2012, 08:02 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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?
• 01-22-2012, 08:49 PM
Fubarable
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!
• 01-22-2012, 09:09 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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

Code:

```import java.util.Scanner; public class Array2 {         public static void main(String[]args)         {                 int number;                 Scanner sj = new Scanner(System.in);                 System.out.println("Please input your three numbers");                 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!
• 01-22-2012, 09:11 PM
Fubarable
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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.
• 01-22-2012, 09:28 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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
Code:

```import java.util.Scanner; public class Array2 {         public static void main(String[]args)         {                 int number;                 Scanner sj = new Scanner(System.in);                 System.out.println("Please input your three numbers");                 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?
• 01-22-2012, 09:29 PM
Fubarable
Re: Getting the sum of the numbers in an array. Help Please!
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++.
• 01-22-2012, 09:35 PM
mike_
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.

Please input your three numbers
1
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Array2.main(Array2.java:20)
Here's the sum 9
• 01-22-2012, 09:37 PM
Fubarable
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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.

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

Let's see your latest code.
• 01-22-2012, 09:39 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

Originally Posted by Fubarable
Let's see your latest code.

Code:

```import java.util.Scanner; public class Array2 {         public static void main(String[]args)         {                 int number;                 Scanner sj = new Scanner(System.in);                 System.out.println("Please input your three numbers");                 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);                                         }                                                                         } }```
• 01-22-2012, 09:43 PM
PhQ
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

Originally Posted by mike_
Code:

```import java.util.Scanner; public class Array2 {         public static void main(String[]args)         {                 int number;                 Scanner sj = new Scanner(System.in);                 System.out.println("Please input your three numbers");                 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 :)
• 01-22-2012, 09:49 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
Quote:

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 :)

Code:

```import java.util.Scanner; public class Array2 {         public static void main(String[]args)         {                 int number;                 Scanner sj = new Scanner(System.in);                 System.out.println("Please input your three numbers");                 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.
• 01-22-2012, 09:50 PM
PhQ
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;
• 01-22-2012, 09:52 PM
JosAH
Re: Getting the sum of the numbers in an array. Help Please!
Yep, and change the body of that for loop to:

Code:

`sum+= numbers1[i]`
kind regards,

Jos
• 01-22-2012, 09:56 PM
mike_
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.
• 01-22-2012, 09:58 PM
JosAH
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
• 01-22-2012, 09:59 PM
mike_
Re: Getting the sum of the numbers in an array. Help Please!
there's 3 nextInt()
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last