0

So I have this program, that is asking the user to enter whether he wants to continue or not, but my problem is, that the while is misbehaving, and it is not waiting for the user input, instead repeats the loop without waiting for the scanner input at the bottom.

public static void main(String[] args) {
    IO init = new IO();
    Scanner scan = init.getScanner();
    String exit = null;
    do {

        System.out.println("Your price");
        int price = scan.nextInt();
        int choice;
        do {
            System.out.println("VAT ");
            System.out.println("1 =  5%");
            System.out.println("2 = 15%");
            System.out.println("3 =  0%");
            choice = scan.nextInt();
        } while (choice==0 || choice>3);

        sazba saz = new sazba(choice);
        double priceVAT = (double)price * saz.sazbaDPH();
        System.out.println("Final price with VAT is: " +priceVAT);    

        System.out.println("Continue YES/NO");
        exit = scan.nextLine();

    } while (!exit.equalsIgnoreCase("no"));
}

I don't see any issue with my do-while logic, but the compiler has some issues with it for some reason.

dotvav
  • 2,696
  • 12
  • 29
The Law
  • 385
  • 3
  • 20
  • 2
    And what are these issues the compiler has? – biziclop Oct 01 '15 at 14:33
  • The program compiles, but as I said, it uses different logic then I expect. At the end of each run it should ask for the user input, but the second to last line never executes, instead it jumps at the top of the statement again – The Law Oct 01 '15 at 14:34
  • what is `sazba` object? – Jordi Castilla Oct 01 '15 at 14:35
  • Why does it not make sense? I tell the program to repeat the loop until user types "no", so until then the program is free to repeat. If user types in "yes", the condition is not met, and it continues from start. But that is not my issue, the line `exit = scan.nextLine();` never executes – The Law Oct 01 '15 at 14:38
  • Whops, sorry, my mistake. I did not read the question to the user. In this case, the condition is correct, but the variable name is misleading (`continue_` would be a better name). Nevermind. – tobias_k Oct 01 '15 at 14:40

0 Answers0