0

Before I begin, I want to thank you all for the help. I would recommend not giving me the code but instead point me to where the mistakes are. Thanks!

I've been trying to figure this issue out for the past few hours and I can't seem to see where I went wrong. I am trying to continue to ask for user input until the user enters -1, which is the sentinel value. After the sentinel value is entered, the program displays the integers entered by the user and their sum. For reasons unknown to me, the printed value is only -1 and the sum is completly off. Here's my code:

import java.util.Scanner;

public class UserSum
{
    public static void main(String args[])
    {
        //prompt user to enter numbers
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter positive integers. Enter -1 to stop. ");
        int integers = userInput.nextInt();

        //sum is initially set to 0
        int sum = 0;

        //execute commands as long as the input does not equal -1
        while(integers != -1)
        {
            //keep gathering user input
            integers = userInput.nextInt();
            sum += integers;
        }

        //print the results to the user
        System.out.println("You entered: " + integers + ", ");
        System.out.println("Sum is: " + sum);
    }
}
6
  • 1
    think about what you are declaring "integers" as. Commented Mar 4, 2015 at 22:03
  • also regardign while(integers >= 0), do you want it to stop if they put negative numbers other than -1? Commented Mar 4, 2015 at 22:06
  • Thanks, Calum. I plan to add a condition to prompt hte user to enter a positive integer if they enter negative integers other than -1. That will be trivial, I think. We'll see how it goes! Commented Mar 4, 2015 at 22:10
  • set initial value of integer to 0 see ideone.com/I6o8DI Commented Mar 4, 2015 at 22:11
  • Integers is a variable that is a single integer. Commented Mar 4, 2015 at 22:13

4 Answers 4

1

The reason your sum value outputs incorrectly is because you read in two inputs before adding to the sum. One read is done entirely before the while loop and one is done before adding to the sum in the while loop. The first value you enter will never be saved in the sum.

Try to implement something similar to the approach below...

int sum = 0;
int value = 0;

while (value != -1) {
   sum += value;
   value = input.nextInt();
   sysout(value);
}

sysout(sum);
Sign up to request clarification or add additional context in comments.

1 Comment

Yep! I actually noticed that before I checked my post for answers. Thanks, Tdorno!
1

An ArrayList is a structure that is greats for all intents and purpose when trying to store multiple values. Google is your friend, ask Google to do you a favor and you'll be on your way with ArrayLists. In your loop, you can then add it, storing all of them.

Comments

0

As per your request I will not post code to help but offer advice on ways you can tackle this.

First off, the way that you handle user inputs is off to a great start, but it might not be 100% correct. I think the 'while' loop is fine. The problem is that you are reading the 'first int', not adding it to the sum, and then reading the 'next int' before you increment the sum by said 'int'.

So in turn I would recommend switching the integers and sum lines in the while loop or initially declaring sum to be equal to integers.

As far as printing all the integers used, you will have to use an array or an array list. You would first determine the number of ints entered by the user, excluding '-1', and declare an array based on that. Then you would read all the integers into the array and sum them at then end. In general I think this would be a more effective method of tackling this.

I hope this helps!

Without arrays

If not using an array, you can use a string and append the strings with the integers that are used to calculate sum

8 Comments

If you switch the integers and sum lines, you should initialize sum to 0 or you'll be summing the first number twice.
@mstbaum Yes, thank you. I meant those as two distinct approaches. Thank you for catching my mistake
Unfortunately, my introductory class has not yet touched upon arrays, so that method to print out the integers entered cannot be used.
@FrakkinShip Well, in that case you can use a string! A string would allow you to add the integers together in order to form what is essentially the user input (without the -1), you can add each number to the string every time you use it. Then at the end, all you have to do is print the string!
That's a good suggestion, msleevi! Good call, mate. Good call.
|
0

You called it integers but it is actually not a list of integers, just the last one read. Check also where you sum up. You do no sum up the first read number, but you sum up the last (-1)... easily to see, because you have two lines where you read the input, that is already suspicious (see the other answer with the loop).

Comments

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.