简体   繁体   中英

How should I handle invalid user input?

For my Restaurant selector program, I am required to write a program that uses decision structures.: I'm getting a couple of errors when I compile and run it such as:

Exception in thread "main" java.util.InputMismatchException
  at java.base/java.util.Scanner.throwFor(Scanner.java:939)
  at java.base/java.util.Scanner.next(Scanner.java:1594)
  at java.base/java.util.Scanner.nextBoolean(Scanner.java:1893)
  at ABaybayanAssignment3.main(ABaybayanAssignment3.java:12)

I was wondering what could I do to my code to fix it. My code can be found below

import java.util.Scanner;

public class ABaybayanAssignment3 {
    public static void main(String[]args) {
        
    Scanner scnr = new Scanner(System.in);
        
    
        System.out.println("Type yes or no for the following questions:");
        
        System.out.println("Is anyone in your party vegetarian?");
        boolean Vegetarian = scnr.nextBoolean();
        
        System.out.println("Is anyone in your party vegan?");
        boolean vegan = scnr.nextBoolean();
        
        System.out.println("Is anyone in your party gluten free?");
        boolean glutenFree = scnr.nextBoolean();
        
        System.out.println("Here are your restaurant choices:");
        
        if(Vegetarian || vegan || glutenFree ) {
            System.out.println("Corner Cafe");
        }
        if(!Vegetarian && !vegan && !glutenFree) {
            System.out.println("Joe's Gourmet Burgers");
        }
        if(!vegan) {
            System.out.println("Main Street Pizza Company");
        }
        if(!vegan && !glutenFree) {
            System.out.println("Mama's Fine Italian");
        }
    }
}

The program compiles and runs without any error.

The problem with your program is you assign yes and no to the boolean variable Vegetarian , vegan , glutenFree at runtime, which is wrong.

Also Unlike C++, a numerical value cannot be assigned to a boolean variable in Java – only true or false can be used.

Furthermore, change

System.out.println("Type yes or no for the following questions:");

TO

System.out.println("Type true or false for the following questions:");

The main issue I think is that you do not handle invalid input errors. Preferably you catch these exceptions and prompt the user to enter a valid input. Optionally, you could also add a default value, that the booleans are set to in the case of nothing being entered by the user (of course specify this at the start of the program).

Something like this should help convey what I mean:

public class ABaybayanAssignment3 {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        System.out.println("Type yes or no for the following questions:");

        boolean Vegetarian = requestBooleanInput(scnr,"Is anyone in your party vegetarian?" );
        boolean vegan = requestBooleanInput(scnr,"Is anyone in your party vegan?");
        boolean glutenFree = requestBooleanInput(scnr,"Is anyone in your party gluten free?");

        System.out.println("Here are your restaurant choices:");

        if (Vegetarian || vegan || glutenFree) {
            System.out.println("Corner Cafe");
        }
        if (!Vegetarian && !vegan && !glutenFree) {
            System.out.println("Joe's Gourmet Burgers");
        }
        if (!vegan) {
            System.out.println("Main Street Pizza Company");
        }
        if (!vegan && !glutenFree) {
            System.out.println("Mama's Fine Italian");
        }
    }

    public static boolean requestBooleanInput(Scanner scanner, String message){
        boolean value = false;
        boolean validInput = false;
        while (!validInput){
            System.out.println(message);
            try {
                value = scanner.nextBoolean();
                validInput = true;
            } catch (Exception e) {
                System.out.println("You entered an invalid input '"+scanner.next()+"', please enter only 'true' or 'false'");
            }
        }
        return value;
    }
}

I have a solution for your program. I used Boolean and used true/false instead of yes/no . Both of them are same in terms of logic. So here's my program.

public class testing {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        System.out.println("Type true or false for the following questions:");

        System.out.println("Is anyone in your party vegetarian?\n");
        boolean Vegetarian = sc.nextBoolean();

        System.out.println("Is anyone in your party vegan?\n");
        boolean vegan = sc.nextBoolean();

        System.out.println("Is anyone in your party gluten free?\n");
        boolean glutenFree = sc.nextBoolean();

        System.out.println("Here are your restaurant choices:");

        if(Vegetarian == true && vegan == true && glutenFree == true) {
            System.out.println("Corner Cafe");
        } else if(Vegetarian == false && vegan == false && glutenFree == false) {
            System.out.println("Joe's Gourmet Burgers");
        } else if(vegan == false) {
            System.out.println("Main Street Pizza Company");
        } else if(vegan == false && glutenFree == false) {
            System.out.println("Mama's Fine Italian");
        }

    }

}

And my output shows this,

Type true or false for the following questions:
Is anyone in your party vegetarian?

true
Is anyone in your party vegan?

true
Is anyone in your party gluten free?

true
Here are your restaurant choices:
Corner Cafe

I have changed the program abit so as to make it easier of course. Due to usage of Boolean, then the only option is true or false. Apart from that, the if-else validation is also modified abit to complement the true/false input. So I hope that answers your question

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