0

So, I've been working on some code for my computer science course to calculate heat index. I'm having troubles loading the data from the text file I'm instructed to work with into the array the program is going to be working with. The block you see commented out is another solution I was trying, which doens't work either... How should I go about fixing it? Thanks!

import java.util.Scanner; 
import java.io.File; 
import java.io.IOException;

public class HeatIndex
{

public static void main(String [] args) throws IOException 
{
Scanner keyWestHumidScan = new Scanner(new File("KeyWestHumid.txt"));
Scanner keyWestTempScan = new Scanner(new File("KeyWestTemp.txt"));

int counter1 = 0;
int counter2 = 0;
int [] keyWestHumid = {};
double [] keyWestTemp = {};
String header1 = "                                     Heat index: Key West, Florida                              ";
String header2 = "\n                                                Months                                        \n          ";
String [] months = {"Jan   ", "Feb   ", "Mar   ", "Apr   ", "May   ", "Jun   ", "Jul   ", "Aug   ", "Sep   ", "Oct   ", "Nov   ", "Dec   ", "Avg   \n"};
String header3 = "*************************************************************************************";
String [] Labels = {"Temp (F)      \n", "Hudimitiy (%)    \n", "HI (F)      "};


//read keyWestHumid into array
while(keyWestHumidScan.hasNext())
{
 keyWestHumid[counter1] = keyWestHumidScan.nextInt();
 counter1++;
 //String data1_parse = keyWestHumidScan.next();
 ///int data1 = Integer.parseInt(data1_parse);
 //keyWestHumid[counter1] = data1;
 //counter1++;
   }
//read keyWestTemp into array
while(keyWestTempScan.hasNext()) 
{
   String data2_parse = keyWestTempScan.next();
   double data2 = Double.parseDouble(data2_parse);
   keyWestTemp[counter2] = data2;
   counter2++;

}
System.out.println(header1);
System.out.print(header2);
for(String headData:months) {
System.out.print(headData);
}
System.out.println(header3);
for(String headData:Labels) {
System.out.print(headData);
}


}
}
2
  • 2
    keyWestTemp is of size 0. If you don't know the size your array will be when you create it, use a List instead (the usual implementation is ArrayList) Commented Oct 25, 2013 at 15:55
  • The exception will tell you which line it occurred on, the index you were trying to get, and the size of the container. Use that to figure it out or at least tell us those 3 things. Commented Oct 25, 2013 at 15:55

4 Answers 4

1

With this line:

double [] keyWestTemp = {};

You have created an array of zero length, so you can't assign anything to it. You can't resize an array, so it's pretty much useless.

You need to establish a size before you use it:

double[] keyWestTemp = new double[10];  // adjust size to fit your actual needs

You'll need to do the same for the keyWestHumid array.

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

Comments

0
int [] keyWestHumid = {};
double [] keyWestTemp = {};

These are arrays with zero size. And trying to assign values to these arrays with out setting the size will result in Out of bounds exception.

You can stick on with the normal array, if you know the max size for the array. As mentioned in Answer 1 by @rgettman

If you are not sure about the max number of elements for the array. use ArrayList. ArrayList will grow automatically, when adding elements.

Comments

0

Use an ArrayList:

import java.util.ArrayList;
ArrayList<Double> keyWestTemp = new ArrayList<Double>();

And it's common methods:

keyWestTemp.add(...);
keyWestTemp.clear();
keyWestTemp.contains(...);
keyWestTemp.get(...);

Comments

0

Use a list if at all possibble.

List<Double> temp = new ArrayList<Double>();
while(keyWestHumidScan.hasNext())
{
 temp.add(keyWestHumidScan.nextInt())
}

However if you really need an array for some reason use the following to get it into an array from the List

double[temp.size()] tempArray; 
int i = 0;
for(Double element:temp){
tempArray[i] = element;
i++
}

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.