1. Curious Java Student Join Date
Sep 2013
Location
Somewhere in the vast valley of nowhere special
Posts
8
Rep Power
0

## Question about sum+=

I have a program, that is supposed to calculate the sum of the numbers, retrieve the smallest of the set, and how many numbers were entered.

But, the sum+= function I have is not properly calculating when negative numbers are entered
Java Code:
```{
public static void main(String [] args)
{
int numberOfInt;
int numberIn;
int small;

int sum = 0;
Scanner input = new Scanner(System.in);

System.out.print("Please, enter the numbers you would like in the set: ");
numberOfInt = input.nextInt();

if(numberOfInt > 0)
{
numberIn = input.nextInt();
small = numberIn;
for(int j = 2 ; j <= numberOfInt ; j++)
{
sum += numberIn;
numberIn = input.nextInt();
if(numberIn < small)
small = numberIn;
}
System.out.println("The sum of set is: " + sum);
System.out.println("The smallest number in the set is: " + small);
System.out.println("The number of numbers entered was: " + numberOfInt);
}
else
System.out.println("The set empty, therefore no sum, smallest number or count exist.");

}
}```
For instance with a data set of -1, -2, and 8. The sum printed should be 5, and my program prints -3.
What function should I use instead of sum+ to fix this tiny problem?  Reply With Quote

2. Member Join Date
Sep 2013
Posts
10
Rep Power
0

## Re: Question about sum+=

Take a look at the logic in your loop. Do it by hand it's a simple mistake and it is not an issue with +=  Reply With Quote

3. Senior Member Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
7

## Re: Question about sum+=

Wouldn't it be easier if you just had a loop that continues until the user chooses to quit? Somewhat like this:

Java Code:
```import java.util.*;

public class Bajs{
public static void main(String[] args){
Scanner hej = new Scanner(System.in);
int x = 1, number, biggest = 0, smallest = 0, sum = 0;
while(hej.hasNextInt()){
System.out.print("Enter number " + (x+1) + ": ");
number = hej.nextInt();
if(x == 1){
biggest = number;
smallest = number;
}else{
if(number > biggest)
biggest = number;
else if(number < smallest)
smallest = number;
}
sum += number;
x++;
}
System.out.print("Sum: " + sum + "\nThe number of numbers entered: " + (x-1) + "\nBiggest number: " + biggest + "\nSmallest number: " + smallest);
}
}```  Reply With Quote

4. Member Join Date
Sep 2013
Posts
10
Rep Power
0

## Re: Question about sum+=

@ Zelaine I don't believe it is an issue of making the code easier or simpler. He/she is having an issue with a number not being added and he/she needs to understand what is happening. In providing your version of the code you eliminate the opportunity of them being able find out the issue they are having. Any issue no matter how simple it may be to you might not be so for them until they have experience in looking for it themselves.  Reply With Quote

5. Curious Java Student Join Date
Sep 2013
Location
Somewhere in the vast valley of nowhere special
Posts
8
Rep Power
0

## Re: Question about sum+= Originally Posted by Ubiquitous Take a look at the logic in your loop. Do it by hand it's a simple mistake and it is not an issue with +=
I think I have found it. Does += need to go after the 22nd line?  Reply With Quote

6. Member Join Date
Sep 2013
Posts
10
Rep Power
0

## Re: Question about sum+=

Going based of your posted code line 22 contains S.o.p("Please enter your next number: "); If you were to put it after this line the issue would still remain but you are close. The issue is you add at the beginning of the loop which is not a good idea considering you get the value later on in the loop.

To clarify think of it as this:
Java Code:
```sum = 0;
counter = 0;
number = userInput; // Number = 5;
while (counter < 2) // Is 0 < 2 ? Yes
sum += number; // sum = 5
number = userInput; // Number = 21
counter++; // counter = 1
whlie (counter < 2) // Is 1 < 2 ? Yes
sum += number; // sum = 26;
number = userInput; // -15;
counter++; // counter = 2;
while (counter < 2) // Is 2 < 2 ? No
Don't run code in loop
S.o.p ("Total sum is: " + sum) // sum = 26```
Hope this makes clarifies as to why the addition at the start of your loop is a bad thing.  Reply With Quote

7. Curious Java Student Join Date
Sep 2013
Location
Somewhere in the vast valley of nowhere special
Posts
8
Rep Power
0

## Re: Question about sum+=

Not sure if I correct the problem the right way, but adding sum += after line 18 seems to have corrected my issue. Thanks for all the help!  Reply With Quote

beginner, java 