I've tested your code and come up with the following:
You might want to check your random values. According to Oracles documentation, Math.random() does the following:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Less than 1.0 means that your maximum values will be less than 4. And by converting your double to int using
(int)you simply cut all decimals, thus your maximum value will be 3. This means that you will only read at most 4 lines (0-3). A suggestion is to use:
Random rnd = new Random(); // Do this once, generates a unique seed int myVal = rnd.nextInt(5); // Generates an integer [0, 4]
- I think what's actually causing your problem is that you increase
cbefore checking if it's bigger or equal to