Using import java.util.Random; nextInt and user input <p>I am fairly new to java so this will probably seem like a basic question. I am trying to use the random java.util and nextInt to create a random number in a range specified by user input and then cast it as a character, to then be stored in an array;</p> <pre><code>gridCells[x][y] = (char)(r.nextInt(numberOfRegions) + 'a'); </code></pre> <p>However, because I want nextInt to use user Input, and although im controlling the range of values, im guessing the error is caused because nextInt thinks numberOfRegions could be 0?</p> <pre><code>// Map Class import java.util.Random; public class map { // number of grid regions private int numberOfRegions; private boolean correctRegions = false // grid constants private int xCord = 13; // 13 so the -1 makes 12 for a 12x12 grid private int yCord = 13; // initiate grid private int[][] gridCells = new int[xCord][yCord]; Random r = new Random(); map() { } // ask for number of regions public void regions() { keyboard qwerty = new keyboard(); // keyboard class while(correctRegions = false) { System.out.print("Please enter the number of regions: "); numberOfRegions = qwerty.readInt(); if(numberOfRegions &lt; 2) // nothing less then 2 accepted { correctRegions = false; } else if(numberOfRegions &gt; 4) // nothing greater then 4 accepted { correctRegions = false; } else { correctRegions = true; } } } // fills the grid with regions public void populateGrid() { for(int x =0; x&lt;gridCells[x].length-1; x++) // -1 to avoid outofboundsexception error { for(int y =0; y&lt;gridCells[y].length-1; y++) { gridCells[x][y] = (char)(r.nextInt(numberOfRegions) + 'a'); } } } public void showGrid() { for(int x=0;x&lt;gridCells[x].length-1; x++) { for(int y=0; y&lt;gridCells[x].length-1; y++) { System.out.print(gridCells[x][y] + " "); } System.out.println(); } } } </code></pre>