我首先要确认用户是否输入了一个值,并确保在取消'被推了。然后,我想在将String转换为java.sql.Date的同时验证String releaseDateString的格式是否正确。
第一次验证正在进行,但随后JOptionPane继续重复,甚至不考虑try和catch跟随它。
这是我的方法
boolean retry = false;
java.sql.Date releaseDate = null;
String releaseDateString = "";
String title = "";
while (!retry) {
while(!retry){//field is validated to make sure a value was entered and to exit if cancel was pushed
releaseDateString = JOptionPane.showInputDialog("Please input the release date of the movie (yyyy-mm-dd)");
qtd.stringValidation(releaseDateString);
}
try { //the date is validated to make sure it is in the correct format
releaseDate = java.sql.Date.valueOf(releaseDateString);
} catch (Exception e) {
retry = false;
JOptionPane.showMessageDialog(null, "Make sure you enter a date in the format of 'dd-mm-yyy'");
}
}
它链接到此方法
public static boolean stringValidation(String attribute){
boolean retry = false;
if (attribute == null){
System.exit(0);
}
else if (attribute.equals("")) //if the cancel button is selected or no value was entered into the
{
JOptionPane.showMessageDialog(null, "Make sure you enter a character into the textbox");
}
else {
retry = true;
}
return retry;
}
答案 0 :(得分:2)
当你这样做时,
qtd.stringValidation(releaseDateString);
您没有将结果分配给retry
。我相信你想要,
retry = qtd.stringValidation(releaseDateString);