0

i have a problem with switch statement in a while(true) loop, basically the text - "Write action (buy, fill, take):" appears twice whenever I choose "buy" or "fill" but only once how its suppose to be when i choose "take", what could be the reason?

boolean active = true;

        while (active) {
            System.out.println("Write action (buy, fill, take):");
            String action = scanner.nextLine();

            switch(action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    int choiceOfCoffee = scanner.nextInt();
                    if (choiceOfCoffee == 1) {
                        waterInMachine -= waterNeededEspresso;
                        coffeeInMachine -= coffeeNeededEspresso;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfEspresso;
                        break;
                    } else if (choiceOfCoffee == 2) {
                        waterInMachine -= waterNeededLatte;
                        milkInMachine -= milkNeededLatte;
                        coffeeInMachine -= coffeeNeededLatte;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfLatte;
                        break;
                    } else if (choiceOfCoffee == 3) {
                        waterInMachine -= waterNeededCappucino;
                        milkInMachine -= milkNeededCappucino;
                        coffeeInMachine -= coffeeNeededCappucino;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfCappucino;
                        break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water do you want to add:");
                    int answer1 = scanner.nextInt();
                    waterInMachine += answer1;
                    System.out.println("Write how many ml of milk do you want to add:");
                    int answer2 = scanner.nextInt();
                    milkInMachine += answer2;
                    System.out.println("Write how many grams of coffee beans do you want to add:");
                    int answer3 = scanner.nextInt();
                    coffeeInMachine += answer3;
                    System.out.println("Write how many disposable cups of coffee do you want to add:");
                    int answer4 = scanner.nextInt();
                    diposableCupsInMachine += answer4;
                    break;
                case "take":
                    System.out.println("I gave you $" + moneyInMachine);
                    moneyInMachine = 0;
                    break;
                case "exit":
                    active = false;
                    break;
            }
        }
Valch
  • 39
  • 5

1 Answers1

2

This should work..

boolean active = true;

        while (active) {
            System.out.println("Write action (buy, fill, take):");
            String action = scanner.nextLine();

            switch(action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    int choiceOfCoffee = Integer.valueOf(scanner.nextLine());
                    if (choiceOfCoffee == 1) {
                        waterInMachine -= waterNeededEspresso;
                        coffeeInMachine -= coffeeNeededEspresso;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfEspresso;
                        break;
                    } else if (choiceOfCoffee == 2) {
                        waterInMachine -= waterNeededLatte;
                        milkInMachine -= milkNeededLatte;
                        coffeeInMachine -= coffeeNeededLatte;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfLatte;
                        break;
                    } else if (choiceOfCoffee == 3) {
                        waterInMachine -= waterNeededCappucino;
                        milkInMachine -= milkNeededCappucino;
                        coffeeInMachine -= coffeeNeededCappucino;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfCappucino;
                        break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water do you want to add:");
                    int answer1 = Integer.valueOf(scanner.nextLine());
                    waterInMachine += answer1;
                    System.out.println("Write how many ml of milk do you want to add:");
                    int answer2 = Integer.valueOf(scanner.nextLine());
                    milkInMachine += answer2;
                    System.out.println("Write how many grams of coffee beans do you want to add:");
                    int answer3 = Integer.valueOf(scanner.nextLine());
                    coffeeInMachine += answer3;
                    System.out.println("Write how many disposable cups of coffee do you want to add:");
                    int answer4 = Integer.valueOf(scanner.nextLine());
                    diposableCupsInMachine += answer4;
                    break;
                case "take":
                    System.out.println("I gave you $" + moneyInMachine);
                    moneyInMachine = 0;
                    break;
                case "exit":
                    active = false;
                    break;
            }
        }
Dhananjay Gupta
  • 296
  • 3
  • 11
  • Thank you, its working. but why its not working with nextInt()? – Valch Feb 06 '20 at 08:50
  • 1
    That has to do with how the Scanner works.. You can see the explanation here - [link](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo?answertab=active#tab-top) – Dhananjay Gupta Feb 06 '20 at 09:05