Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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]);

    }

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #5
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #7
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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);
    		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!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by mike_ View Post
    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. #9
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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);
    		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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #11
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by mike_ View Post
    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.

  13. #13
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    Let's see your latest code.
    Java 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);
    			
    		}
    		
    		
    		
    		
    	}
    
    }

  14. #14
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

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

    Quote Originally Posted by mike_ View Post
    Java 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 :)

  15. #15
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    Quote Originally Posted by PhQ View Post
    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);
    		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.

  16. #16
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default 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. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

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

    there's 3 nextInt()

Page 1 of 2 12 LastLast

Similar Threads

  1. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  2. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  3. Swapping numbers in an array
    By Awt582 in forum New To Java
    Replies: 3
    Last Post: 11-01-2010, 12:03 AM
  4. How do I place these numbers into an array
    By mainy in forum New To Java
    Replies: 2
    Last Post: 07-19-2009, 06:57 PM
  5. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM

Posting Permissions

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