gpt4 book ai didi

java - 数据验证中的数据验证

转载 作者:行者123 更新时间:2023-11-30 09:04:24 24 4
gpt4 key购买 nike

我首先要验证用户是否输入了一个值,并确保在按下“取消”时退出。然后,我想在将字符串转换为 java.sql.Date 的同时验证字符串 releaseDateString 的格式是否正确。

第一个验证正在进行,但随后 JOptionPane 继续 self 重复,甚至不考虑紧随其后的 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;
}

最佳答案

当你这样做的时候,

qtd.stringValidation(releaseDateString); 

您没有将结果分配给 retry。我相信你想要,

retry = qtd.stringValidation(releaseDateString);

关于java - 数据验证中的数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com