2

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;


    try{

        String strSearch = searchSerialField.getText();

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

        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
                        System.out.println("Saved");
                        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.

juiceb0xk
  • 949
  • 3
  • 19
  • 46
  • Using a common `flag` for both loop and button click can be handy. Update `flag` on button click handler and use the same `flag` in the inner most loop where you wanna `break`. Using new `Thread` will make it effective. –  Nov 20 '16 at 23:11

2 Answers2

1

You can leave the for loop with break;. Remember row, column, etc before leaving and start the loops with the remembered values.

I think it would be a good idea to put the for loop into its own method. The same applies for the "popup" for the input. Then you can just call them when necessary. ^^-d

Edit: some example code:

        package pauseLoop;


public class pauseLoop {
        int rows;
        int columns;
        int something;
        static int[] a;

    public static void main(String[] gnarrr){
        pauseLoop myLoop = new pauseLoop();

        myLoop.Loop();

        myLoop.Loop();
    }

    public pauseLoop(){
        this.rows = 0;
        this.columns = 0;
        this.something = 11; //just random data
    }

    public void Loop(){
        System.out.println("starting loop with: rows = " + rows + " columns = " + columns + " something = " + something);

        for(int count = rows; count < 99999 ;count++){
            /* do some black magic ;) */

            if(count % 23 == 22){ // !condition to pause the loop! i chose something that can occur more than once
                this.rows += count; //remember rows
                this.columns += 5; // columns got 5 more by some black magic (nested loop or so)
                this.something += 31; //just some data needed to be remembered for later
                System.out.println("leaving loop with: rows = " + rows + " columns = " + columns + " something = " + something+ "\n");
                break; //leave loop!
            }
        }
    }

}

Yields the following output if run:

starting loop with: rows = 0 columns = 0 something = 11

leaving loop with: rows = 22 columns = 5 something = 42

starting loop with: rows = 22 columns = 5 something = 42

leaving loop with: rows = 44 columns = 10 something = 73

Happy coding!

0

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Once the dialog box opens and asks for the user input, I still need to pause the for loop, otherwise the code will continue as if I have not entered anything in the text field. – juiceb0xk Nov 20 '16 at 04:54
  • @juiceb0xk, yes the loop pauses while the JOptionPane/JDialog is visible and will restart as soon as the dialog closes. So you need to get the data and do your processing with it when the dialog closes before you continue your loop. – camickr Nov 20 '16 at 20:16