0

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!!

2 Answers 2

4

When you do:

int[] month = {tempCount};

This creates an Array of only one element, tempCount. You wanted:

int[] month = new int[tempCount];

Which will create an Array of size tempCount


Also note that this loop:

for (int i = 0; i < 3; i++) {
    input.nextLine();
}

is calling nextLine() without resolving it to anything.

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..

Yes this will be correct since you always set it to i, which incremented any time you add to any of the Array's. I would recommend using List's, so that you can simply use the add() method and add it to the end of the List. Also ArrayList's don't have a fixed size, so you wouldn't have to declare the size beforehand.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow what a stupid mistake! Thank you! The first integer in the text file is the number of data entries (hence tempCount), the following 3 lines are purely for the text document's formatting. My intention is for that loop to skip over those lines so it picks up on the line starting with the data. Is this incorrect?
@SamPhinney No that is not incorrect, I just thought you might have been by accidentally skipping over important data.
1

you cannot do like this i <= tempCount; since you define array only allow from 0 to less than tempCount;

should for (int i = 0; i < tempCount; i++) <= only less than here or you have to increase the array size(1st point)

Second point is int[] month = {tempCount}; that mean you only define that array have 1 element? It not correctly how you define the array; should be something like this int[] month = new int[2]; // create an month array store 2 elements

Hope it help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.