2

I'm trying to add all the elements together in an array that was decided through user input, Every time I run the code that I've constructed below I get a number that is obviously not the sum of the elements. What am I doing wrong?

import java.util.Scanner;
public class SumProduct
{
    public static void main (String []args)
    {
        Scanner input = new Scanner (System.in);
        int[] array1 = new int [input.nextInt()];
        input = scan.nextInt();        
        for (int i = 0; i < array1.length; i++)
        {
            array1[i] = input.nextInt();
        }
        for (int i = 0; i < array1.length; i++)
        {
            int j = array1[i];
            int k = array1[i]+1;
            int sum = j + k;
            System.out.print(sum);
        }
    }
}
0

2 Answers 2

2

You probably want to prompt the user to enter the size of the array if you're going to do this.This line of code is allowing whatever the user enters to be the size of your array.

int[] array1 = new int [input.nextInt()]; //this sets the size of the array through user input

scan doesn't exist in the currrent context:

input = scan.nextInt();   // this is invalid syntax as scan is not the Scanner you created    
for (int i = 0; i < array1.length; i++)
{
    array1[i] = input.nextInt();
}

I would do this to keep adding elements to the array:

// no need for this: input = scan.nextInt();
for (int i = 0; i < array1.length; i++)
{
    System.out.println("Enter integer to add:");
    array1[i] = input.nextInt();
}

This code will give you the sum of the elements if you just add one element at a time instead of two to the sum variable:

int sum = 0;
for (int i = 0; i < array1.length; i++)
{
    sum += array1[i];
    System.out.print(sum);  // this will print out sum after each addition
}
System.out.print(sum);  // this will print out sum after the entire array is summed

Adding some logic to only allow the user to enter so many numbers to the array would be helpful as well. You will need to move on from them entering data into the array at some point. Then also remember to close the scanner when you're finished getting data from the user:

input.close();
Sign up to request clarification or add additional context in comments.

2 Comments

How can the program print out sum if sum is outside the for loop?
Ah ok, it works now thanks. The reason I didn't prompt the user to enter the values was that my work is graded by a computer and it'll count it wrong if it sees extra code like that. Next time I'll make sure to put it in though if I'm asking a question on here.
2

Your problem is in the following lines of code:

for (int i = 0; i < array1.length; i++)
    {
        int j = array1[i];
        int k = array1[i]+1;
        int sum = j + k;
        System.out.print(sum);
    }

it should look something like

int sum = 0;
for (int i = 0; i < array1.length; i++)
    {
        sum = sum + array1[i];
    }
System.out.print(sum);

The first change is to declare the variable "sum" outside of the loop. The way it's written, it will get declared, then disappear, then declared, then disappear for every loop iteration. You also probably want to initialize it to 0

The second change is to your summation logic. Lets assume your array contains the three numbers [1, 2, 3] and walk through your logic.

j = array1[0]     //(j == 1)
k = array1[0] + 1 //(k == 1 + 1 == 2)
sum = j + k       //(sum == 1 + 2 == 3)

You then throw out the variable "sum" like I mentioned earlier, and start over with the same logic on the second array element, then again on the third.
i.e. j = 2, k = 2+1, sum = 2 + 2 + 1. followed by j = 3, k = 3 + 1, sum = 3 + 3 + 1.

You can probably see how this isn't the sum, and results in you logging the three digits 3, 5, and 7 for this example case. In my updated logic, we simply loop through each element and add it to the current running total.

hope this helps

3 Comments

Yes, it definitely helps thank you. just one more question though is it possible to do the same code put replace the + symbol with a * symbol to multiply the elements instead of adding them?
yes, but you would want to initialize "sum" to 1 instead of 0. also there's a handy syntax trick I omitted in my answer: sum += array[i] is the same as saying sum = sum + array[i] It also works with multiplication: sum *= array[i]
Ah ok, makes sense why I was getting zero now haha. thanks a lot this was very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.