Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to iterate a string in order to validate values are in range using charAt()?
    primarykey
    data
    text
    <p>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. </p> <pre><code> //Entering by ROWS //This is for a 4x4 board size using rows if (dataSelection == 1) { if (boardSize == 1) { int row = 1; while (row &lt; 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 &lt; row1Values4x4.length(); i++) { char c = row1Values4x4.charAt(i); if (row1Values4x4.length() == 7 &amp;&amp; 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 &lt; strArray.length; i++) { arraySidesInteger[i] = Integer.parseInt(strArray[i]); } fourArray[row-1] = arraySidesInteger; for (int i = 0; i &lt; fourArray.length; i++) { for (int j = 0; j &lt; fourArray.length; j++) System.out.print(fourArray[i][j] + " "); System.out.println(); } row++; } </code></pre> <p>Please let me know if there </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload