简体   繁体   中英

How to break out of while loop while continuing to iterate in nested for loop

so what I am trying to do is validate that my user is only entering the numbers 1-4 using commas with no duplicates. For example, 1,2,3,4 or 2,1,4,3. I have included a while loop in order to make sure the user inputs the correct information. I am getting confused when it comes to the for loop because I am not sure whether or not I need to have an additional for loop in order to access that the array within the array using i and j? I am really confused and I am seeking some kind of assistance. I currently only have experience in Python.

I realize that when I break out of the while loop to input the values into the array, once I return to ask the user for the next row's values, my for loop's initialization and update has restarted.

I have also attempted to change my if statement to: if (row1Values4x4.charAt(k) > '0' && row1Values4x4.charAt(k) < '5') { however, that does not make sense because I want to break out of the loop when my for loop comes across a value that is not in the range of 1-4.

I am not getting any errors, it is not working the way I want it to.

import java.util.Scanner;

public class sudokuPractice {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[][] fourArray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
            int row = 1;
            int k = 0;
            while (row < 5) 
            {
                String row1Values4x4 = "-1";
                boolean done = false;
                while (!done)
                {
                    Scanner firstRow4x4 = new Scanner(System.in);
                    System.out.println("Please enter four values using commas for row " + row); //this needs to loop
                    row1Values4x4 = firstRow4x4.next();
                    row1Values4x4 = row1Values4x4.replaceAll(" ",""); //this is in case user enters numbers with spaces
                    if (row1Values4x4.length() == 7) {
                        for (k = 0; k < row1Values4x4.length();k++) {
                                if (row1Values4x4.charAt(k) < '0' && row1Values4x4.charAt(k) > '5') {
                                    done = true;
                                } //else {
                                //}
                            }
                    }
                }
                String strArray[] = row1Values4x4.split(",");
                int arraySidesInteger[] = new int[strArray.length];
                for (int i = 0;  i < strArray.length;  i++) {
                    arraySidesInteger[i] = Integer.parseInt(strArray[i]);
                }
                fourArray[row-1] = arraySidesInteger;
                for (int i = 0; i < fourArray.length; i++) {
                    for (int j = 0; j < fourArray.length; j++)
                        System.out.print(fourArray[i][j] + " ");
                    System.out.println();
                }
                row++;
                k++;
                }
}

}

//I included the integer k because I was trying to retain the initialization and update of my for loop.

Could please try this

public static void main(String[] args) {

    int row = 1;
    int k = 0;
    String row1Values4x4 = "-1";
    int[][] arraySidesInteger = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };

    while (row <= 4) {

        boolean done = false;

        while (!done) {

            Scanner firstRow4x4 = new Scanner(System.in);
            System.out.println("Please enter four values using commas for row " + row); // this needs to loop
            row1Values4x4 = firstRow4x4.next();
            String[] values = row1Values4x4.split(",");

            for (int i = 0; i < values.length; i++) { //Logic to validate the value

                if (Integer.parseInt(values[i]) < 0
                        || Integer.parseInt(values[i]) > 4) {
                    System.out
                            .println("You have entered the value which not fit in the boundary.Please Try again.");
                    done = true;
                    break;
                }
            }

            if (done) { // Loop again to get the row value correctly.
                done = false;
                continue;
            }

            //Adding the value to row
            for (int i = 0; i < values.length; i++) {
                arraySidesInteger[row - 1][i] = Integer.parseInt(values[i]);
            }

            row++;
            done = true;

        }
    }

    //Prints the array
    for (int i = 0; i < arraySidesInteger.length; i++) {
        for (int j = 0; j < arraySidesInteger.length; j++)
            System.out.print(arraySidesInteger[i][j] + " ");
        System.out.println();
    }

}

To break execution of any level of loop is pretty easy if to use labels

look on example

int count=0;

A: while(true){
    while(true){
        System.out.println(count++);
        if(count==10){
            break A;
        }
    }
}

first loop is marked as A and break stops execution of block of code marked as A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM