-3

I am having trouble using values from a switch statement in a separate method that I have below the main method. How would I get it so I can use the values I am trying to return from the switch statement? Here is my code:

private static int itemNo;
private static double userQuantity;
private static double oneTotal, twoTotal, threeTotal, fourTotal, fiveTotal;
private static double result;

public static void main(String[]args){
    double totalAmount = 0;

    Scanner input = new Scanner(System.in);

    double tA = calculateTotal(itemNo);
    totalAmount += result;

while(true){
    System.out.print("Please enter the item number you wish to use, or enter -1 to stop: ");
    int itemNo = input.nextInt();

    if(itemNo == -1){
        System.out.println("The total value of the purchase is: " + totalAmount);
        break;
    }

    System.out.println("Now please input the quantity of the item you specified above: ");
    double userQuantity = input.nextInt();

}   
    }

public static double calculateTotal(int itemNo){
    double result = 0;

        switch(itemNo){
        case 1:
            double oneTotal = 2.98 * userQuantity;
            break;
        case 2:
            double twoTotal = 4.50 * userQuantity;
            break;
        case 3:
            double threeTotal = 9.98 * userQuantity;
            break;
        case 4:
            double fourTotal = 4.49 * userQuantity;
            break;
        case 5:
            double fiveTotal   = 6.87 * userQuantity;
            break;
        }
    result = oneTotal + twoTotal + threeTotal + fourTotal + fiveTotal;
    return result;
}

I am trying to get it so it allows the user to input what item number they would like to use, and then the quantity of that item they bought, and then return the value that is calculated in the switch statement to be used in result, which is the printed out once they type "-1" to stop the program. However totalAmount is staying at zero, and is not taking the returned result value.

Fyree
  • 87
  • 1
  • 1
  • 10

2 Answers2

1

Variables in Java are scoped to the nearest { } block, which in this case is the switch statement. This means the doubles you've defined are only in scope inside the switch.

Instead of the pattern you're doing currently, keep a total value outside the switch, initially set to 0, and replace your variable declarations (double oneTotal = etc.) with total +=. Then simply return total.

Some related questions on variable scoping:


I see also that your input processing is broken; you read the input into itemNo repeatedly, but you never call calculateTotal() after reading the input. Probably as simple as moving the calculateTotal() call after the nextInt() calls, but you may need to experiment with that a little.

Community
  • 1
  • 1
dimo414
  • 42,340
  • 17
  • 131
  • 218
  • `total = ` will work just fine. There is a `break` in every `case` in the code. – Dima Maligin Feb 16 '15 at 22:04
  • 1
    @DimaMaligin absolutely; your suggested refactoring would presumably also work and is cleaner. I'm trying to suggest the minimally invasive change, in case there's more going on that we aren't being shown, and in the hope that OP understands the suggestion. The more you change under someone's feet, the less they're likely to understand. OP can probably just paste in your method, but will they understand why it works? – dimo414 Feb 16 '15 at 22:08
0

Hmm... you have break in each case all your variables are redundant.

This should work just as good:

public static double calculateTotal(int itemNo){

        switch(itemNo){
        case 1:
            return (2.98 * userQuantity);
        case 2:
            return (4.50 * userQuantity);
        case 3:
            return (9.98 * userQuantity);
        case 4:
            return (4.49 * userQuantity);
        case 5:
            return (6.87 * userQuantity);
        default:
            return 0;
        }
}
dimo414
  • 42,340
  • 17
  • 131
  • 218
Dima Maligin
  • 1,172
  • 2
  • 9
  • 27
  • Why would I return 0? – Fyree Feb 16 '15 at 22:02
  • @Fyree I donno you tell me it's your code. If no matching `case` is found you return 0. – Dima Maligin Feb 16 '15 at 22:03
  • @Fyree suppose you type in `6` rather than `1`-`5` - what should your code do in that case? Returning `0` is as good a fallback as any. – dimo414 Feb 16 '15 at 22:04
  • 1
    Personally for readability I'd just use a default case, as it makes your intentions a little more clear. – user2366842 Feb 16 '15 at 22:05
  • I want it to return the value that it calculates for the user. – Fyree Feb 16 '15 at 22:05
  • @user2366842 noted. Edited! – Dima Maligin Feb 16 '15 at 22:07
  • @Fyree the `return 0` will only run if no `case` was matched. if any of the `case`'s are matched it'll return the correct value vrom the `case` it self. – Dima Maligin Feb 16 '15 at 22:08
  • @Dima Malignin Are you able to explain to me how this would be able to used in the main method? I want it to be able to print out what the result was calculated when the user enters "-1" to stop the while loop. Also thanks for explaining what the default value did to me, at first I was not sure why you threw it in there. I still am unable to figure out why it returns 0 as the total, even when the user enters an itemNo of 1-5, and the enters a userQuantity that is not zero. – Fyree Feb 16 '15 at 22:10
  • To expand on that a little further - although you'd LIKE for the user to always input a valid value, the honest truth is in a real world scenario, there's a good number of times where that won't actually happen. You're effectively telling the program what to do with a value that's unaccounted for. If you feed it say, a 7, and it's not accounted for, the function still needs to return SOMETHING. A default statement (or a return value outside of the switch statement as was originally posted) will tell the program what to do with the value - in this case, you'll return a zero if it's not valid. – user2366842 Feb 16 '15 at 22:11
  • @user2366842 Alright thank you for the information. I was going to add something to stop the user from doing that, but default makes it easier than what I was going to do. Do you know why, even when I input a valid 1-5 number along with a quantity of the number, that it returning zero when they stop the while-loop (I feel like it is something with the switch statement not correctly taking in the userQuantity value to calculate what it needs to return). Also I need to return a result so I can cumulate the data they input. – Fyree Feb 16 '15 at 22:18
  • @Fyree It seems to me that you do not fully understand how code executes. In short any code under a `return` statement(if the statement runs) will NOT execute since control will be returned to the method caller. Thats beyond the scope of this answer so i wont go over it in depth. – Dima Maligin Feb 16 '15 at 22:19