Below are the beginnings of a program I am writing to read temperature information from weather.txt, which lists date and temperature data in this format: 01/01/1941 38 25 and represents the date, min temp, and max temp respectively. The first entry in weather.txt is the total number of data entries, and the next 3 lines exist for formatting:
import java.io.*;
import java.util.*;
public class WeatherAnalysis {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("weather.txt"));
input.useDelimiter("[/ \t\n\r]+");
int tempCount = input.nextInt();
int[] month = {tempCount};
int[] day = {tempCount};
int[] year = {tempCount};
int[] tmax = {tempCount};
int[] tmin = {tempCount};
System.out.println("There are " + tempCount + " entries.");
for (int i = 0; i < 3; i++) {
input.nextLine();
}
int count = 0;
for (int i = 0; i <= tempCount; i++) {
if (count < 5) {
switch(count) {
case 0:
month[i]=input.nextInt();
break;
case 1:
day[i]=input.nextInt();
break;
case 2:
year[i]=input.nextInt();
break;
case 3:
tmax[i]=input.nextInt();
break;
case 4:
tmin[i]=input.nextInt();
break;
}
count++;
}
else {
count = 0;
}
}
}
}
I have initialized 5 integer arrays. Since the data is formatted statically, I intend to read each piece of data through a fixed format. For that, I have created a switch statement with a counter.
My thoughts are that the count variable will find the corresponding case, add the element to the array, break, and increment the counter before getting the next input. Using Eclipse's debugger, I can see that my i and count variables increment correctly.
However, as soon as the debugger reads day[i]=input.nextInt(); I am met with...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at WeatherAnalysis.main(WeatherAnalysis.java:29)
Admittedly I am having a bit of a hard time grasping arrays. I know mine are initialized poorly. For starters, they are FAR too high (there are 30k data entries), but I hesitate to set it to an arbitrary value for fear of it being too small.
Additionally...using this method I think I am going to have a lot of random jumps if I print out my array. I believe elements are inititally set to 0, so my thoughts are that my arrays may end up like 0,0,0,46,0,0,0,42 etc..
Any insight would be very appreciated. This is my 2nd post here so excuse any formatting, and thank you!!