while 循环与 JOptionPane 返回 StringIndexOutOfBounds 异常



在我的代码中,我有一个while循环,中间嵌套了3个IF测试,这些测试具有由ELSE触发的标志:

[test1] 检查输入值的长度是否正好为 1 [阻止用户不输入任何内容]

[test2] 检查索引 0 处的输入值是否为数字 [我需要一个数字作为输入,但我使用的是 JSWING]

[test3] 检查输入值长度是否大于 1 [2 位 (10,11,12,...(

num1= JOptionPane.showInputDialog(null,"Please input Guess #" + (counter+1), "0"); 
while(exit == false || test1 == false || test2 == false || test3 == false) {
if(num1.length() < 1) {
JOptionPane.showMessageDialog(null,"Input required");
num1= JOptionPane.showInputDialog(null,"Please input Guess #" + (counter+1), "0");
}
else {
test1 = true;
}
if(Character.isDigit(num1.charAt(0)) == false) {
JOptionPane.showMessageDialog(null,"Input has to be a number between 0 - 9.");
num1= JOptionPane.showInputDialog(null,"Please input Guess #" + (counter+1), "0");
}
else {
test2 = true;
}
if(num1.length() > 1) {
JOptionPane.showMessageDialog(null,"Input has to be a number between 0 - 9.");
num1= JOptionPane.showInputDialog(null,"Please input Guess #" + (counter+1), "0");
}
else {
test3 = true;
}
if(test1 == true && test2 == true && test3 == true) {
exit = true;
}

我遇到的问题介于第一次和第二次测试之间。当我尝试输入任何内容作为值 ["/或只有一个空框并按 Enter 时,它会检测到什么都没有的错误并显示"需要输入"一次,但是一旦循环,它会为第二次试验输出 StringIndexOutOfBoundsException 它适用于我尝试过的所有其他情况(没有输入 ->正确,无输入 ->不正确......只有顺序无输入情况才会使程序崩溃。

据说错误在这一行中,但我不明白在哪里或如何。

if(Character.isDigit(num1.charAt(0)) == false)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:709)
at oof.Lottery_Swing_FIX.main(Lottery_Swing_FIX.java:56)

固定逻辑

JOptionPane.showMessageDialog(null,"Enter 3 one-digit positive numbers for your 3 guesses");
for(int counter = 0; counter < LIMIT; counter++) {
test = false; 
while(exit == false || test == false) {
num1= JOptionPane.showInputDialog(null,"Please input Guess #" + (counter+1), ""); 
if(num1.length() < 1 || Character.isDigit(num1.charAt(0)) == false || num1.length() > 1) {
JOptionPane.showMessageDialog(null,"Integer between 1-9 required");
}
else {
test = true;
}
if(test == true) {
numberInput = Integer.parseInt(num1);
exit = true;
}
else {
continue;
}
}

您的"修复"不处理空值,如果对话框关闭 (x( 或选择了取消取消按钮,JOptionPane.showInputDialog((将返回该值。你不能像使用NullString ("(那样对String#length((方法玩null,所以你需要在你的代码中检查这一点,否则你最终会得到一个 NullPointerException。您可以在第一个if语句中作为条件组件执行此操作:

if (num1 == null) {
// The CANCEL or dialog close button was selected.
}

你真的不需要代码中的那些布尔标志。事情会发生,或者根本不会发生。你真的不需要旗帜来提醒你离家这么近(可以这么说(。如果您在if语句中正确建立了条件,如果不需要,则使用 else它们。话虽如此,在while循环的条件中也不需要这些布尔标志。

while 循环需要关注一件事...提示提供了有效数据。如果不是,则保存提示数据的变量 (num1( 将转换为空字符串 ("(。所以在现实中:

String num1 = "";
while (num1.equals("")) { .... }

因此,只要num1包含一个空字符串 ("(,我们就会继续循环,从而重新提示正确的输入。

在代码中,您希望向用户提供有关其输入失败原因的特定详细信息。有几种方法可以执行此操作,但是无论您选择哪种方式,请确保它不会生成任何最终可能停止应用程序或更改其准确性能的异常(错误(。使用 if 和else if语句在其当前用例中执行此特定任务并没有错。遵循您的特定主题:

int LIMIT = 3, numberInput;
int[] guesses = new int[LIMIT];
String errMsg;
String num1;
JOptionPane.showMessageDialog(null, "<html>You will be prompted three times "
+ "to supply<br>a positive <font color=red><b>single digit</b></font> "
+ "number.</html>", "Information", JOptionPane.INFORMATION_MESSAGE);
for (int counter = 0; counter < LIMIT; counter++) {
num1 = "";
while (num1.equals("")) {
errMsg = "";
num1 = JOptionPane.showInputDialog(null, "Please input Guess #" + (counter + 1), 
"Guess #" + (counter + 1),JOptionPane.QUESTION_MESSAGE);
// Does num1 contain null?
if (num1 == null) {
if (JOptionPane.showConfirmDialog(null,
"<html>You <font color=blue>canceled</font> your input!<br>"
+ "Do you want to quit?</html>", "Quit",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
num1 = ""; // Set for re-prompt.
continue;
}
// Is nothing supplied?
else if (num1.length() < 1) {
errMsg = "<html><font size=5 color=red><center>Nothing Supplied!"
+ "</center></font><br>You must provide a single Integer "
+ "value between<br>0 and 9 (inclusive).</html>";
}
// Is too much supplied?
else if (num1.length() > 1) {
errMsg = "<html><center><font size=5 color=red>To Much Supplied!</font><br>" +
"<font size=5 color=blue>"" + num1 + ""</font></center><br>" +
"You must provide a single Integer value between<br>0 and 9 "
+ "(inclusive).</html>";
}
// Is the supplied character a number?
else if (!Character.isDigit(num1.charAt(0))) {
errMsg = "<html><center><font size=5 color=red>Invalid Digit Supplied!"
+ "</font><br><font size=5 color=blue>"" + num1 + ""</font>"
+ "</center><br>You must provide a single Integer value "
+ "between<br>0 and 9 (inclusive).</html>";
}
// Does errMsg actually contain a message? If so display it.
if (!errMsg.equals("")) {
JOptionPane.showMessageDialog(null, errMsg, "Invalid Input!", 
JOptionPane.WARNING_MESSAGE);
num1 = ""; // Set for re-prompt.
}
else { 
numberInput = Integer.parseInt(num1);
// ... do whatever you want to do with numberInput, for example ....
guesses[counter] = numberInput;
}
}
}
// Display User's LIMITED guesses:
StringBuilder sb = new StringBuilder();
sb.append("<html>The <font color=red><b>").append(LIMIT).
append("</b></font> Guesses supplied by User are:<br><br>");
for (int i = 0; i < guesses.length; i++) {
sb.append("Guess #").append((i + 1)).append(":  <font color=blue>").append(guesses[i]).append("</font><br>");
}
sb.append("</html>");
JOptionPane.showMessageDialog(null, sb.toString(), "Guesses Provided", 
JOptionPane.INFORMATION_MESSAGE);

如您所见,向用户提供有关输入失败的详细信息需要更多的代码。如果您决定只想要一个简单的"无效输入!"消息,则可以删除所有这些代码。这实质上迫使用户以更大的强度阅读提供的提示,例如:

int LIMIT = 3;
int numberInput;
int[] guesses = new int[LIMIT];
String num1;
JOptionPane.showMessageDialog(null, "<html>You will be prompted three times "
+ "to supply<br>a positive <font color=red><b>single digit</b></font> "
+ "number.</html>", "Information", JOptionPane.INFORMATION_MESSAGE);
for (int counter = 0; counter < LIMIT; counter++) {
num1 = "";
while (num1.equals("")) {
num1 = JOptionPane.showInputDialog(null, "Please input Guess #" + (counter + 1), 
"Guess #" + (counter + 1),JOptionPane.QUESTION_MESSAGE);
// Does num1 contain null?
if (num1 == null){
if (JOptionPane.showConfirmDialog(null,
"<html>You <font color=blue>canceled</font> your input!<br>"
+ "Do you want to quit?</html>", "Quit",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
num1 = ""; // Set for re-prompt.
}
else if (!num1.matches("\d")) {
JOptionPane.showMessageDialog(null, "<html><center><font size=5 color=red>Invalid Input Supplied!</font><br>" +
"<font size=5 color=blue>"" + num1 + ""</font></center><br>" +
"You must provide a single Integer value between<br>0 and 9 "
+ "(inclusive).</html>", "Invalid Input!", JOptionPane.WARNING_MESSAGE);
num1 = "";
}
else { 
numberInput = Integer.parseInt(num1);
// ... do whatever you want to do with numberInput, for example ....
guesses[counter] = numberInput;
}        
}
}
// Display User's LIMITED guesses:
StringBuilder sb = new StringBuilder();
sb.append("<html>The <font color=red><b>").append(LIMIT).
append("</b></font> Guesses supplied by User are:<br><br>");
for (int i = 0; i < guesses.length; i++) {
sb.append("Guess #").append((i + 1)).append(":  <font color=blue>").append(guesses[i]).append("</font><br>");
}
sb.append("</html>");
JOptionPane.showMessageDialog(null, sb.toString(), "Guesses Provided", 
JOptionPane.INFORMATION_MESSAGE);

特别注意上述代码中else if语句 (!num1.matches("\d)( 的条件。这里使用 String#matches(( 方法与一个小的正则表达式一起使用,即"\d"表达式。此表达式告诉matches(( 方法查看我们匹配的字符串(在 num1中(是否是单个数字的字符串数值(例如:"5"(。那么,其他 if语句在问什么:

否则,如果num1中包含的字符串不是(!( 个位数的字符串数值,则在我的大括号 ({...}( 中运行代码。这基本上涵盖了除 null 之外的所有输入失败(因为我们将其作为退出选项处理(,并且 String#matches(( 方法将不接受 null。

如果你不想要一个退出选项,那么你只需要在代码中有一个if语句:

if (num1 == null || !num1.matches("\d")) { ... }

相关内容

  • 没有找到相关文章

最新更新