没有发现错误

时间:2018-03-22 22:21:20

标签: java

我遇到的问题是我的java程序的try catch部分。当用户输入1-4之间的数字时,它可以工作。但是当用户输入的数字不是它没有捕获错误并打印出错误文本。如果有帮助,我会将整个程序放入...任何帮助将不胜感激。

package student;

import java.util.Scanner;

public class RegistryCLI {

    private Registry theRegistry;
    private final Scanner in = new Scanner(System.in);

    public RegistryCLI(Registry theRgistry) {
    }

    public void doMenu() {
        boolean shouldRun = true;

        while (shouldRun) {
            System.out.println("Registry Main Menue");
            System.out.printf("*******************\n");
            System.out.println("1. Add Student");
            System.out.println("2. Delete a Student");
            System.out.println("3. Print Registry");
            System.out.println("4. Quit");
            //        System.out.println("Slect option [1 , 2 , 3 , 4] :>" + input1);
            int choice = getValidNumericInput(4);

            switch (choice) {
                case 1:
                    doAddStudent();
                    break;
                case 2:
                    doDeleteStudent();
                    break;
                case 3:
                    doPrintRegistry();
                    break;
                case 4:
                    shouldRun = false;
                    break;
            }
        }
    }

    private void doAddStudent() {
        boolean shouldRun = true;
        while (shouldRun) {
            System.out.println("Add New Student");
            System.out.println("**************\n");
            System.out.println("Enter student ID: ");
            String id = in.nextLine();
            System.out.println("Enter forename: ");
            String fName = in.nextLine();
            System.out.println("Enter surname: ");
            String lName = in.nextLine();
            System.out.println("Enter degree scheme: ");
            String degree = in.nextLine();
            theRegistry.addStudent(new Student(fName, lName, id, degree));
            System.out.println("Enter another? (Y/N): ");
            if (in.nextLine().toLowerCase().equals("n")) {
                shouldRun = false;
            }
        }

    }

    private void doDeleteStudent() {
        System.out.println("Delete s Student");
        System.out.println("**************\n");
        System.out.printf("\n");
        System.out.println("");

    }

    private void doPrintRegistry() {
    }

    private int getValidNumericInput(int maxVal) {
        boolean validInput = false;

        int choice = 0;

        while (!validInput) {
            System.out.println("Select option between 1 and " + maxVal);
            String input1 = in.nextLine();
            try {
                choice = Integer.parseInt(input1);
                validInput = true;
            } catch (NumberFormatException e) {
                System.out.println("WARNING: Invalid input");
                validInput = false;
            }
        }

        return choice;
    }

}

2 个答案:

答案 0 :(得分:1)

发生这种情况的原因是因为在logging.info('Status is %s' % response.status, extra={'status': response.status}) 中,仅当getValidNumericInput()的值是方法input1无法解析为整数的字符串时才会引发错误。由于此方法不知道程序中的“最大值”是什么,因此它将识别任何有效整数(无论它是否高于“最大值”)字符串为有效,因此不会抛出错误。

修复此问题的方法是在解析整数之后进行检查,以确定整数Integer.parseInt()是否在您的逻辑范围内:

choice

答案 1 :(得分:0)

在你的程序中,NumberFormatException将捕获不是数字的输入。

当您输入的数字不是1-4时,不会抛出任何异常,因此程序只会回到循环的开头。