Scanner tempTXT = new Scanner(new File("KeyWestTemp.txt")); Scanner humidTXT = new Scanner(new File("KeyWestHumid.txt"));
double[] temp = new double[11]; double[] humid = new double[11]; double[] heatIndex = new double[11];
for (int i = 0; tempTXT.hasNextDouble(); ++i) { temp[i] = tempTXT.nextDouble(); }
Why is this for loop running into an out of bounds exception?
husoski2015-09-26T19:05:18Z
Favorite Answer
If you have more than 11 numbers, that will happen.
You can stop early with a small change to the for loop:
for (int i = 0; tempTXT.hasNextDouble() && i<temp.length; ++i) { ...
That will stop when the array fills up. You can detect when that happens after the loop ends, if you like. If tempTXT.hasNextDouble() is still true, then there is unread data still in the Scanner.