使用 JOptionPane 时如何跳出 for 循环?
How can I break out of a for loop when using JOptionPanes?
我有一个很大的问题,我一直在努力解决这个问题,但我终其一生都无法解决这个问题。从我目前收集到的信息来看,当我使用 JOptionPanes 时我无法退出我的 for 循环,因为它出于某种原因永远不会到达它,即使代码就在下面。
我的代码的目标是遍历 .CSV 文件,用户将输入一系列输入,最值得注意的是,我正在检查重复项,如果找到重复项,那么我将继续显示 JOptionPane.showMessageDialog,并在消息显示后中断 for 循环。
但是,我的消息对话框一直在循环,不会再继续下去,所以我一直在做错事。
这是我的代码:
JOptionPane.showMessageDialog(null, myPanel, "Edit Fruit", JOptionPane.INFORMATION_MESSAGE);
for (row = 0; row < myEntries.size(); row++)
{
for (column = 0; column < myEntries.get(row).length; column++)
{
//If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText()))
{
//break loop
JOptionPane.showMessageDialog(null, "Serial already exists!");
break;
//If the serial does not exist, write this new line to the end of the file
}else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText())))
{
System.out.println(row);
myEntries.add(new String[] {newSerial.getText(), dateFormat.format(newDate), newLocation.getText(),
"",newAllocation, newCondition, newType.getText(), newComments.getText()});
}
}
如果我的文档包含“6”,并且我搜索了它,则此代码将出于某种原因遍历这两个 if 语句,并且不会中断。
我的意思是,我的 for 循环将遍历我的文本文件,直到找到与用户输入完全匹配的内容,如果是,则中断。如果未找到重复项,则将新代码行附加到文件末尾。
它只是不工作,我不知道从这里去哪里,所以我希望这里有人可以提供帮助。
如果您需要更多信息,请告诉我,以便我提供并解决此问题。
谢谢。
你还需要一个break
。
for (row = 0; row < myEntries.size(); row++)
{
for (column = 0; column < myEntries.get(row).length; column++)
{
//If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText()))
{
//break loop
JOptionPane.showMessageDialog(null, "Serial already exists!");
break;
//If the serial does not exist, write this new line to the end of the file
} else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText())))
{
System.out.println(row);
myEntries.add(new String[] {newSerial.getText(), dateFormat.format(newDate), newLocation.getText(), "",newAllocation, newCondition, newType.getText(), newComments.getText()});
}
}
break; // <- This is required to break from outer loop.
}
如果你打算break
第一个循环,你可以使用labels
OUTER: for (row = 0; row < myEntries.size(); row++) {
for (column = 0; column < myEntries.get(row).length; column++) {
// If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText())) {
// break loop
OptionPane.showMessageDialog(null, "Serial already exists!");
break OUTER;
// If the serial does not exist, write this new line to the end of the file
} else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText()))) {
System.out.println(row);
myEntries.add(new String[] { newSerial.getText(), dateFormat.format(newDate), newLocation.getText(), "", newAllocation, newCondition, newType.getText(), newComments.getText() });
}
}
}
我有一个很大的问题,我一直在努力解决这个问题,但我终其一生都无法解决这个问题。从我目前收集到的信息来看,当我使用 JOptionPanes 时我无法退出我的 for 循环,因为它出于某种原因永远不会到达它,即使代码就在下面。
我的代码的目标是遍历 .CSV 文件,用户将输入一系列输入,最值得注意的是,我正在检查重复项,如果找到重复项,那么我将继续显示 JOptionPane.showMessageDialog,并在消息显示后中断 for 循环。
但是,我的消息对话框一直在循环,不会再继续下去,所以我一直在做错事。
这是我的代码:
JOptionPane.showMessageDialog(null, myPanel, "Edit Fruit", JOptionPane.INFORMATION_MESSAGE);
for (row = 0; row < myEntries.size(); row++)
{
for (column = 0; column < myEntries.get(row).length; column++)
{
//If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText()))
{
//break loop
JOptionPane.showMessageDialog(null, "Serial already exists!");
break;
//If the serial does not exist, write this new line to the end of the file
}else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText())))
{
System.out.println(row);
myEntries.add(new String[] {newSerial.getText(), dateFormat.format(newDate), newLocation.getText(),
"",newAllocation, newCondition, newType.getText(), newComments.getText()});
}
}
如果我的文档包含“6”,并且我搜索了它,则此代码将出于某种原因遍历这两个 if 语句,并且不会中断。
我的意思是,我的 for 循环将遍历我的文本文件,直到找到与用户输入完全匹配的内容,如果是,则中断。如果未找到重复项,则将新代码行附加到文件末尾。
它只是不工作,我不知道从这里去哪里,所以我希望这里有人可以提供帮助。
如果您需要更多信息,请告诉我,以便我提供并解决此问题。
谢谢。
你还需要一个break
。
for (row = 0; row < myEntries.size(); row++)
{
for (column = 0; column < myEntries.get(row).length; column++)
{
//If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText()))
{
//break loop
JOptionPane.showMessageDialog(null, "Serial already exists!");
break;
//If the serial does not exist, write this new line to the end of the file
} else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText())))
{
System.out.println(row);
myEntries.add(new String[] {newSerial.getText(), dateFormat.format(newDate), newLocation.getText(), "",newAllocation, newCondition, newType.getText(), newComments.getText()});
}
}
break; // <- This is required to break from outer loop.
}
如果你打算break
第一个循环,你可以使用labels
OUTER: for (row = 0; row < myEntries.size(); row++) {
for (column = 0; column < myEntries.get(row).length; column++) {
// If serial exists
if (myEntries.get(row)[column].trim().equals(newSerial.getText())) {
// break loop
OptionPane.showMessageDialog(null, "Serial already exists!");
break OUTER;
// If the serial does not exist, write this new line to the end of the file
} else if (!(myEntries.get(row)[column].trim().equals(newSerial.getText()))) {
System.out.println(row);
myEntries.add(new String[] { newSerial.getText(), dateFormat.format(newDate), newLocation.getText(), "", newAllocation, newCondition, newType.getText(), newComments.getText() });
}
}
}