我正在使用此方法来确定来自 javafx 中 TextField 的输入字符串是否具有此模式 AB123CD 与模式 ("\D{2}\d{3}\D{2}") 我正在使用 try catch 附件,它捕获(手动)抛出的 PatternSyntaxException。我问这个,因为 PatternSyntaxException 使用 String String Integer 构造函数,显示异常,例如:索引int ^ 处的错误或类似的东西 我的问题是我无法弄清楚如何获得正确的索引以放入构造函数,或者我是否可以使用任何其他异常来代替
这是代码的一部分:
try {
if(!tfTarga.getText().matches("\\D{2}\\d{3}\\D{2}"))
throw new PatternSyntaxException(tfTarga.getText(), tfTarga.getText(), 0);
else {
this.olCCar.add(new CCar(new ContractCars(new Contract(this.comboCont.getValue()), this.tfTarga.getText(), LocalDate.now(), Integer.parseInt(this.tfPrezzo.getText()))));
this.tfTarga.setText("");
this.tfPrezzo.setText("");
}
} catch (PatternSyntaxException e) {
alert("Error", "Format Error", e.getLocalizedMessage());
}