0

The error is in indexoutofboundsexception at System.out.println(hurricane.get(2));. How can I fix this so I can print a hurricane object? I tried several different ways but to no avail. I tried adding null to hurricane but that didn't work. It also says the index 2 is out of bounds for length 2.

import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
public class HurricaneTesterTest

{

  public static void main(String[] args) throws IOException {

    //read data from text file & put in an array
    File fileName = new File("HurricaneData.txt");
    Scanner inFile = new Scanner(fileName);
    int numValues = 0;

    //count number of lines in text file
    while (inFile.hasNextLine()) {
      inFile.nextLine();
      numValues++;
    }
    inFile.close();

    //initialize arrays based on lines counted in text file
    ArrayList < Integer > years = new ArrayList < Integer > ();
    ArrayList < String > months = new ArrayList < String > ();
    ArrayList < Integer > pressures = new ArrayList < Integer > ();
    ArrayList < Double > windSpeeds = new ArrayList < Double > ();
    ArrayList < String > names = new ArrayList < String > ();
    ArrayList < String > Cat1 = new ArrayList < String > ();

    //read and assign data from text file to the arrays
    inFile = new Scanner(fileName);
    int index = 0;
    while (inFile.hasNext()) {
      years.add(inFile.nextInt());
      months.add(inFile.next());
      pressures.add(inFile.nextInt());
      windSpeeds.add(inFile.nextDouble());
      names.add(inFile.next());
      index++;
    }
    inFile.close();

    for (int i = 0; i < windSpeeds.size(); i++) {
      windSpeeds.set(i, windSpeeds.get(i) * 1.151);

      //System.out.println(windSpeeds);
    }

    for (int x = 0; x < windSpeeds.size(); x++) {
      if (windSpeeds.get(x) < 96 && windSpeeds.get(x) > 73) {
        Cat1.add("Category 1");
      }
      if (windSpeeds.get(x) < 111 && windSpeeds.get(x) > 95) {
        Cat1.add("Category 2");
      }
      if (windSpeeds.get(x) < 130 && windSpeeds.get(x) > 110) {
        Cat1.add("Category 3");
      }
      if (windSpeeds.get(x) < 157 && windSpeeds.get(x) > 129) {
        Cat1.add("Category 4");
      }
      if (windSpeeds.get(x) > 156) {
        Cat1.add("Category 5");
      }
    }
    ArrayList < Hurricane > hurricane = new ArrayList < Hurricane > ();

    for (int a = 0; a < hurricane.size(); a++) {
      hurricane.add(null);
    }

    for (int r = 0; r < hurricane.size(); r++) {
      hurricane.add(new Hurricane(years.get(r), names.get(r), months.get(r), Cat1.get(r), pressures.get(r), windSpeeds.get(r)));;
    }
    System.out.println(hurricane.get(2));

  }
}

//create a Hurricane ArrayList using the data above

//user prompted for range of years

//print the data
Seabyte
  • 369
  • 1
  • 2
  • 13
  • The first element of a list is **0**, the second element of a list is **1**, in a list of length 2, there is no index **2** (which would be the third element) – Mark Rotteveel Apr 22 '21 at 16:13

1 Answers1

0
ArrayList<Hurricane> hurricane = new ArrayList<Hurricane>();

for(int a = 0; a<hurricane.size(); a++){
    hurricane.add(null);
}

for(int r = 0; r<hurricane.size(); r++){
      hurricane.add(new Hurricane(years.get(r),names.get(r),months.get(r),Cat1.get(r),pressures.get(r), windSpeeds.get(r)));;
}

According to these lines you are adding nothing to the list because it is empty initialized. Please try something like this:

ArrayList<Hurricane> hurricane = new ArrayList<Hurricane>();

for(int r = 0; r<years.size(); r++){
      hurricane.add(new Hurricane(years.get(r),names.get(r),months.get(r),Cat1.get(r),pressures.get(r), windSpeeds.get(r)));;
}

It should work in your case. You will have probably issues with Cat1 because it looks like if blocks has wrong ranges comparison: cat1(73-96), cat2(95-111) - overlaps.

ByerN
  • 123
  • 7