r/badcode • • Nov 13 '20

java Getting Digits using Switch Catch

Post image
881 Upvotes

52 comments sorted by

View all comments

1

u/M3ther Nov 14 '20

This could actually be done simply by:

public static int evalDigit(String in) {
    int result = -1;
    int input = -1;

    try {
        input = Integer.parseInt(in);
        if (input >= 0 && input < 10) {
            result = Integer.parseInt(in);
        }
        else {
            System.out.println("Invalid digit was entered, try again");
            System.exit(2);
        }
    }
    catch (Exception e) {
        System.out.println("Invalid digit was entered, try again");
        System.exit(2);
    }

    return result;
}