简介
我在捕获InputMismatchException
时遇到问题。当我已经搜索InputMismatchException
时,我已经阅读了该网站上显示在首页上的所有主题。在阅读这些主题时,我找到了我的一个问题的答案;该答案告诉我,我必须在导入语句中添加import java.util.InputMismatchException;
。但是,我遇到了一个其他问题,在任何解决方案中都找不到。最接近的其他问题是this one。我认为他和我有同样的问题,但是该解决方案对我不起作用。
我的问题
我正在制作一个类,演示我制作的ProductionWorker数据类型。我想测试所有输入,以确保它们是可以按预期操作的有效输入。 我试图获得双倍的薪资输入,如果输入错误,我想被要求再次尝试输入。说我不小心输入了“加里”而不是我的工资。我想了一次try-catch-finally最终可以工作,但是进入catch块之后,我仍然收到InputMismatchException ...
控制台输出
run:
What is your name: gary
Please enter employee number: 88-m
Incorrect employee ID, please check it and try again.
Enter employee number: 888-m
Please enter hire date as "MM/DD/YYYY": 07/14/1994
Please enter shift (1 for day shift, 2 for night shift.: 2
Please enter rate of pay: $h
Rate of pay can only be digits.
Exception in thread "main" java.util.InputMismatchException
Examples: 30.00; 22.10; 7.25; 10.00
at java.util.Scanner.throwFor(Scanner.java:864)
Please enter rate of pay: $ at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at programmingchallengeq1.ProgrammingChallengeQ1.main(ProgrammingChallengeQ1.java:69)
Picked up _JAVA_OPTIONS: -Xmx512m
C:\Users\nikru\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 15 seconds)
在我看来,由于该打印语句显示在控制台中,因此我将其放入catch块中,并且我认为如果将其放入catch块中,则异常不会使程序崩溃。我不知道我在做什么错...
代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package programmingchallengeq1;
import java.util.Scanner;
import java.util.regex.*;
import java.util.InputMismatchException;
public class ProgrammingChallengeQ1 {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
final int SHIFT_LENGTH = 12;
ProductionWorker workman = new ProductionWorker();
System.out.print("What is your name: ");
workman.set_name(kb.nextLine());
System.out.print("Please enter employee number: ");
// ask user for id and save it to workman
workman.set_id(kb.nextLine());
// while loop ensures that employee num matches specific format
while (check_id(workman) == false)
{
System.out.print("Incorrect employee ID, please check it and" +
" try again.\n" + "Enter employee number: ");
workman.set_id(kb.nextLine());
}
System.out.print("Please enter hire date as \"MM/DD/YYYY\": ");
// ask user for date of hire and save it to workman
workman.set_hire_date(kb.nextLine());
// attempt to ensure a valid date was input with while loop
while(check_date(workman) == false)
{
System.out.println("Date not recognized, please check date" +
" and try again.\n" + "Enter hire date as \"MM/DD/YYY\": ");
workman.set_hire_date(kb.nextLine());
}
System.out.print("Please enter shift (1 for day shift, 2 for night"+
" shift.: ");
workman.set_shift(kb.nextInt());
// the while loop checks if a correct value is stored for shift
while(workman.get_shift() != 1 && workman.get_shift() != 2)
{
System.out.print("Unrecognized shift. Please enter a 1 for" +
" day shift, or 2 for night shift: ");
workman.set_shift(kb.nextInt());
}
System.out.print("Please enter rate of pay: $");
// attempt to ensure a valid rate of pay is input with try-catch
// boolean is the exit condition for while loop, which only breaks
// if the input is valid
boolean valid_input = false;
while (valid_input != true)
{
try
{
workman.set_pay_rate(kb.nextDouble());
valid_input = true;
}
catch(InputMismatchException ex)
{
//System.out.println(ex.getMessage());
System.out.print("Rate of pay can only be digits.\nExamples:" +
" 30.00; 22.10; 7.25; 10.00\nPlease enter rate of pay: $");
}
finally
{
workman.set_pay_rate(kb.nextDouble());
}
}
}
public static boolean check_id(ProductionWorker wrkr)
{
// creating a regex pattern to check against input employee ID
// to avoid inputting error
String id_format = "\\d{3}-[a-m]";
Pattern id_pattern = Pattern.compile(id_format);
// check id against compiled regex pattern and save to a bool
Matcher id_matcher = id_pattern.matcher(wrkr.get_id());
boolean id_matches = id_matcher.matches();
return id_matches;
}
public static boolean check_date(ProductionWorker wrkr)
{
//same concept as in check_id()
String date_format = "\\d{2}/\\d{2}/\\d{4}";
Pattern date_pattern = Pattern.compile(date_format);
Matcher date_matcher = date_pattern.matcher(wrkr.get_hire_date());
boolean date_matches = date_matcher.matches();
return date_matches;
}
}
感谢所有帮助,谢谢!
答案 0 :(得分:0)
您也在nextDouble()
块中运行finally
,因此无论发生什么情况,它都将在此时失败。基本上,第一个失败的调用不会消耗数据,因此第二个调用不会等待新的用户输入,它会立即尝试使用已经存在的输入并再次失败。
您必须明确使用该行来解决此问题:
Scanner sc = new Scanner(System.in);
try {
sc.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Oops, something went wrong...");
sc.nextLine();
sc.nextDouble();
}
我删除了finally
块,因为我不明白为什么在第一次尝试成功后为什么要重试该操作。
答案 1 :(得分:0)
finally
{
workman.set_pay_rate(kb.nextDouble());
}
您的异常在这里被抛出,并且没有被捕获,因为该语句没有被try \ catch包围