I have a huge problem that I've been working on and for the life of me, cannot get this right. From what I have gathered as of now, I am unable to exit my for loop when I'm using JOptionPanes, because it will never reach it for some reason, even when the code is right underneath.
The goal of my code is to iterate through a .CSV file and the user will input a series of inputs, most notably, I'm checking for duplicates, and if a duplicate is found, then I will proceed to display a JOptionPane.showMessageDialog, and break the for loop after the message has displayed.
However, my message dialog keeps looping and won't go any further, so I'm stuck on as what I'm doing wrong.
Here's my code:
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()});
}
}
If my document contains a "6", and I search for that, this code will iterate through both if statements for some reason, and just won't break.
What is meant to happen is, my for loop will iterate through my text file until it finds an exact match from the users input, and if so, break. If a duplicate is not found, then append the new line of code to the end of the file.
It's just not working and I'm not sure where to go from here, so I'm hoping someone here can help.
If you need any more information, then please let me know so I can provide and get this problem fixed.
Thank you.
If you intend to break
the first loop, you can use 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() });
}
}
}