简体   繁体   中英

How to iterate a string in order to validate values are in range using charAt()?

I am trying to iterate a string that contains the users inputed values. I want to validate that the user only enters 4 characters and that all of them are between 1 and 4. For example, prompts the user to enter 4 values using commas, therefore they can only enter 1,2,3,4. If they enter anything else, then they will be asked again. I have included the section of my code where I am trying to perform the validation. I am also experiencing an unreachable code error which does not make sense to me. This takes place after I close the while (true) loop.

        //Entering by ROWS
    //This is for a 4x4 board size using rows
        if (dataSelection == 1) {
        if (boardSize == 1) {
            int row = 1;
            while (row < 5) 
            {
                String row1Values4x4 = "-1";
                while (true) 
                {
                    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
                    for (int i = 0; i < row1Values4x4.length(); i++) {
                        char c = row1Values4x4.charAt(i);
                        if (row1Values4x4.length() == 7 && c == 48) //I entered 48 in order to test if it is using ascii value (48 = 0) {
                            break;
                        }
                    }
                } //I think I need to include another break in order to escape the second loop? 
                String strArray[] = row1Values4x4.split(","); //This is where I get an unreachable code error 
                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++;
                }

Please let me know if there

Your comment is right; you need a second break in there. The break that exists only breaks out of the for loop, but not the while loop.

Perhaps instead of

while (true) 
{
    // do some stuff
    for (/* some other stuff */) 
    {
        // even more stuff
        if (/* should we break */) 
        {
            break;
        }
    }
}

you could try something like

boolean done = false;
while (!done) 
{
    // do some stuff
    for (/* some other stuff */) 
    {
        // even more stuff
        if (/* should we break */) 
        {
            done = true;
            break;
        }
    }
}

Why not use the split method in strings

String s = userInput;
String[] inputArray = s.split(",");
for(int i = 0; i < inputArray.length; i++){
    // check if the character is correct here
}

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