Java异常处理问题

时间:2018-04-24 01:29:04

标签: java exception-handling

我面向对象编程的最终项目是一个程序,它使用对象,类和用户输入来计算用户是否能够根据他们的收入以及用户定义的月收入百分比来计算总体拥有成本。这辆车不会超过。该程序按需要运行,但我需要实现异常处理以捕获不正确的输入(即双字段中的字符串字符)。我的导师和我无法弄清楚如何正确捕获异常并提示用户重新输入适当的值。

以下是我需要处理异常的代码:

System.out.println("What is the price of the car?");
    car.setPrice(sc.nextDouble());
    System.out.println("What is your down payment?");
    car.setDownPayment(sc.nextDouble());
    car.setOutTheDoor();
    System.out.println("Please enter your interest rate (APR) as a decimal value. For example, an interest rate of 3% would be entered as '.03.'");
    car.setRate(sc.nextDouble());
    System.out.println("What is the maturity of your loan? The maturity of your loan is the length of time in months over which the loan will be paid.");
    car.setMaturity(sc.nextInt());
    car.setPayment();

    System.out.println("The car you've chosen is a" + car.getModelName());
    System.out.println("The MSRP is: $" + car.getStickerPrice());
    System.out.println("The down payment is: $" + car.getDownPayment());
    System.out.println("The OTD price is: $" + car.getOutTheDoor());
    System.out.println("The interest rate is: " + car.getRate());
    System.out.println("The maturity is: " + car.getMaturity());
    System.out.println("Your monthly payment is: $" + car.getPayment());

    System.out.println("Let's see if you can afford a " +car.getModelName() + "!");
    while(personalFinance.getSalary() <= 0)
    {
        System.out.println("What is your annual salary after taxes and deductions?");
        personalFinance.setSalary(sc.nextDouble());
        if(personalFinance.getSalary() <= 0)
        {
            System.out.println("You need to make money to afford a car! Please reenter a value that is greater than 0.");
        }       
    }


    System.out.println("Approximately how many miles do you drive a year?");
    car.setMilesDriven(sc.nextInt());
    System.out.println("What is the average fuel efficiency of the " + car.getModelName() + "?");
    car.setFuelEfficiency(sc.nextInt());
    System.out.println("What is the average cost of fuel in your area?");
    car.setPricePerGallon(sc.nextDouble());
    System.out.println("What is your annual insurance rate?");
    car.setInsurance(sc.nextDouble());
    System.out.println("What is the percentage of your monthly income that you do not want your monthly cost of ownership to exceed? Enter the percent as decimal value. For example, if you do not want your monthly payment to exceed 20% of your monthly income, then enter .20.");
    personalFinance.setMonthlySalary(sc.nextDouble());

1 个答案:

答案 0 :(得分:0)

如果您使用的是Java 1.8,则可以创建一个通用的辅助方法来处理当用户输入一些无法转换为所需类型的字符串时发生的InputMismatchException

public static <T> T nextValid(final String prompt, final Scanner sc, final Function<Scanner, T> nextFn) {
    do {
        System.out.println(prompt);
        try {
            return nextFn.apply(sc);
        } catch (InputMismatchException e) {
            System.out.println("Invalid input, please try again.");
            sc.next(); // skip invalid token.
        }
    } while (true);
}

你这样使用它:

int x = nextValid("Give me an integer:", sc, Scanner::nextInt);
double y = nextValid("Now a double:", sc, Scanner::nextDouble);

然后它会一直询问,直到用户输入有效信息。