WesternFive
WesternFive

Reputation: 11

Storing values not working?

I'm using an array for a project to store currency values, as well as a double variable to hold a running total. When I run my code through the loop, the user input is not stored in the array, and nothing is added to the running total. When the user inputs a -1, it is supposed to break the loop and calculate taxes etc, and when a 0 is inputed, the last value is removed from the array. No matter what I do, I cannot get these values into the array, or the running total to work. I am sure what I am doing wrong is something stupid, but I cannot spot it.

for(i = 0; i < priceArray.length; i++) {
    System.out.print("\nEnter the price of the item...");
    userInput = input.nextDouble();
    if(userInput == -1) { // This will break the user out of the loop.
        break;
    }
    else if(userInput == 0.0) {
        System.out.println("You entered a zero, removing last price of $" + priceArray[i] + ".");
        i--;
        runningTotal =- priceArray[i];
    }
    else if(userInput > 0.0 && userInput < 2999.99) {
        priceArray[i] = userInput;
        priceArray[i] += runningTotal;
        userInput += runningTotal;
        System.out.println("You entered $" + userInput + ", total is $" + runningTotal + ".");
    }
    else {
        i--;
        System.out.println("Please enter a valid value under $2999.99.");
    }// End if.
};// End for

Upvotes: 1

Views: 55

Answers (2)

digitaljoel
digitaljoel

Reputation: 26574

In the case where you attempt to remove a value rour running total is going to break. runningTotal =- priceArray[i]; is going to set the value to the negative of the value you are trying to remove. You should use -= instead of =-.

In the case where you attempt to add a value you are also messing up the running total.

priceArray[i] = userInput;
priceArray[i] += runningTotal;
userInput += runningTotal;

I'm not sure what you think is happening on these lines. You set the value of the array at the given index to what was input, which is great. Then you override the value by adding the runningTotal to it, which isn't what you want. Then you are overwriting the input value by adding runningTotal to it, which also isn't what you want. You want to set the value within the array, hten add the value to the runningTotal, and that's it.

Upvotes: 0

Gille
Gille

Reputation: 5773

A couple of things are wrong here

1) When you calculate running total you do it incorrectly (you don't calculate it at all):

priceArray[i] = userInput;
priceArray[i] += runningTotal;
userInput += runningTotal;

It should be this:

priceArray[i] = userInput; /* Save the price */
runningTotal += userInput; /* Increment the total */

Now you will have incremented runningTotal and saved the price correctly.

2) When you remove something (entering 0) you also do it wrong. You print the next empty value, which will be zero and then negate instead of subtracting.

i--; /* Step back one step */
System.out.println("You entered a zero, removing last price of $" + priceArray[i] + ".");
runningTotal -= priceArray[i];
i--; /* The for-loop will increment i for us, so we must subtract one extra time */

Upvotes: 1

Related Questions