juiceb0xk juiceb0xk - 1 year ago 82
Java Question

Waiting for a button press within a for loop?

I'm not sure how to get around this, but the purpose of my code is to iterate through a csv file and the user searches for a certain item. I have my items read through a string array of type list with CSVReader, so that's not a problem.

When a user searches for an item, I have created a for loop to iterate through the rows and columns of my csv file, and once found will output some code.

What I'm wanting to do, is once the item has been found, it will open up another frame and again the user will make an input through the use of a text field. Then another button is to be pressed and thus continues where we left off in our other for loop in order to execute some code that makes changes. I can only iterate through the for loop once, or else the row and column values will double upon doing so twice.

Here's my code:

editTigerFrame obj = new editTigerFrame();

String getLocation = obj.editLocationField.getText();
javax.swing.JButton saveButt = obj.saveEditButton;


String strSearch = searchSerialField.getText();

CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
List<String[]> myEntries = reader.readAll();

for (int row = 0; row < myEntries.size(); row++)
for (int column = 0; column < myEntries.get(row).length; column++)
if (myEntries.get(row)[column].trim().equalsIgnoreCase(strSearch))
System.out.println("Found - Your item is on row: " + row + ", column: " + column);

//If serial is found, open the editTigerFrame to edit details
new editTigerFrame().setVisible(true);

//Replace with user input
//When button is pressed in other class, execute this code
String newLocation = getLocation;

As you can see near the bottom of the code snippet, once the button is pressed on the other form, execute the code to change a string.

Is there a way to pause and continue within a for-loop?

Thank you for your help, and if you need anything else, please let me know and I will provide.

Answer Source

Is there a way to pause and continue within a for-loop?

Maybe display a JOptionPane asking for using input?

Check out the section from the Swing tutorial on How to Make Dialogs for more information and examples.

If you have more complex data to gather then you would use a modal JDialog.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download