简体   繁体   中英

How to prevent a user from entering a invalid answer

I have tried to stop the user from entering anything besides yes or no however when they do it just keeps on saying error try again forever.

public static void main(String[] args) {
    System.out.println
            ("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    Boolean flag = true;
    while (flag == true) {
        if ("yes".equalsIgnoreCase(TempAnswer)) {
            System.out.println("Yes");
            flag = false;
        } else if ("no".equalsIgnoreCase(TempAnswer)) {
            System.out.println("No");
            flag = false;
        } else {
            System.out.println("Error Enter Again!!");
        }
    }
}

You're never re-assigning the input value to your TempAnswer variable in the while loop and loop runs infinitely, as you never give the chance to the boolean value to be changed, based on user's input.

Few side-notes:

  1. Use Java Naming Conventions and define your member variable identifiers with camelCase , not with PascalCase ;
  2. Do not introduce redundant code. Use while(true) instead of defining boolean variable flag and using it as the condition; correspondingly, use break; instead of setting flag to false.

Eventually, I would have rewritten your code as follows:

public static void main(String[] args) {
    System.out.println("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String tempAnswer = "";
    while (true) {
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
            tempAnswer = target.nextLine();
        }
    }
}

Get rid of the superfluous calls and make sure that you read the answer in the loop. If you don't do that, you'll indefinitely loop because you're not changing the loop-condition.

Use break to break the loop instead of using flags for such small scope.

Also, don't use Boolean , use boolean instead. You will probably never have to use Boolean , so don't use it.

public static void main(String[] args) {
    System.out.println("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    while (true) {
        String tempAnswer = target.nextLine();
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        }
        if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        }
        System.out.println("Error Enter Again!!");
    }
}

Try to break the loop when the correct answer is typed

public static void main(String[] args) {
    System.out.println
        ("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    while (True) {
        if ("yes".equalsIgnoreCase(TempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(TempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM