简体   繁体   中英

Java index out of bound error

I am having issues with index out of bounds in my program. The for loop in the method at the bottom is supposed to run through as many times as specified in the flightSeatingAmount() method. But I keep getting an error when running the program to test. Sometimes I get one or two outputs before it errors out and other times I don't get any output, just the error. Here is my code:

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class OLE1 {

static String[] airports = {"LAX", "MSP", "FAR", "ATL", "ORD", "DFW", "DEN", "JFK", "SFO", "CLT", "LAS", "PHX", "IAH", "MIA", "PEK", "CAN", "HND", "HKG", "SIN", "LHR", "CDG", "LGW", "MUC", "FRA", "EDI"};
static final int numberOfEntries = 10000;
static String[] firstNames = {"Isis", "Donnette", "Reyes", "Willis", "Kathy", "Elizbeth", "Long", "Jim", "Devorah", "Magda", "Maryetta", "Keturah", "Corrinne", "Shena", "Xiao", "Otha", "Hallie", "Pennie", "Dong", "Kristopher", "Eveline", "Ardella", "Tien", "Tianna", "Loren", "Many", "Anjelica", "Cecile", "Mae", "Jenae", "Sonya", "Dotty", "Florance", "Mittie", "Katia", "Nena", "Lu", "Janee", "Armando", "Leandro", "Claris", "Claudine", "Moriah", "Eddie", "Susan", "Rhoda", "Monnie", "Emelia", "Cory", "Ying"};
static String[] lastNames = {"Belva", "Nam", "Liz", "Jeanett", "Corine", "Abe", "Olga", "Olevia", "Ernestine", "Joanne", "Sharyn", "Heidi", "Zachariah", "Sylvester", "Luetta", "Stephaine", "Garrett", "Debby", "Judi", "Noe", "Maybelle", "Eldora", "Roseann", "Madge", "Glayds", "Eleonore", "Josephine", "Quincy", "Alyson", "Earlene", "Clementina", "Jeri", "Kristel", "Carrol", "Zona", "Eileen", "Margherita", "Joline", "Terence", "Christinia", "Eldon", "Arleen", "Aimee", "Chanda", "Carin", "Prudence", "Tanja", "Kathlene", "Kareen", "Geneva"};
static String[] middleInitial = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
static int day = (int)(Math.random() * 21);

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

    System.out.println("Welcome to the airplane program. This program will go ahead and create a list of all the passengers that have gone through 25 different airports.");
    System.out.println("The list of the airports we keep track of are listed below\n");

    //for listing out the airport names
    int count = 0;
    for (int i = 0; i < 5; i++) {
        System.out.print(airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.println(" | " + airports[count]);
        count++;
    }

    flightManifest();

}

public static int flightSeatingAmount() {
    int flight = (int)(50 + (Math.random() * 800));
    return flight;
}

public static String nameGenerator() {
    String names = "";
    int firstAndLast = (int)(Math.random() * 50);
    int initial = (int)(Math.random() * 26);
    names = lastNames[firstAndLast] + "," + firstNames[firstAndLast] + "," + middleInitial[initial];
    return names;
}

public static int flightTimeIntervel() {
    int timing[] = {15, 30, 45, 00};
    int randomTiming = (int)(Math.random() * 4);
    return timing[randomTiming];
}

public static int airportSelectionOne() {
    int airportOne = (int)(Math.random() * 49);
    return airportOne;
}

public static int airportSelectionTwo() {
    int airportTwo = (int)(Math.random() * 49);
    return airportTwo;
}

public static void flightManifest() throws FileNotFoundException {

    System.out.println(flightSeatingAmount());
    for (int i = 0; i <= flightSeatingAmount(); i++) {
        System.out.println(nameGenerator() + "," + "0" + "," + airports[airportSelectionOne()]);
    }

}

}

Here is the output I get:

Welcome to the airplane program. This program will go ahead and create a list of all         the passengers that have gone through 25 different airports.
The list of the airports we keep track of are listed below

LAX | MSP | FAR | ATL | ORD
DFW | DEN | JFK | SFO | CLT
LAS | PHX | IAH | MIA | PEK
CAN | HND | HKG | SIN | LHR
CDG | LGW | MUC | FRA | EDI
843
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45
    at OLE1.flightManifest(OLE1.java:70)
    at OLE1.main(OLE1.java:33)

The 843 is referring to how many times the loop is supposed to execute.

Your airportSelectionOne returned 45. There aren't 46 airports in your airports array. This means you went well past the end of the array, which is illegal. Change your function so it doesn't ever return a number higher than the size of the array. Hint: you can tell the size of the array by airports.length

更改以下代码以具有正确的数组大小。

int airportOne = (int)(Math.random() * [size of airport array - 1]);

You are having problem with this loop:

for (int i = 0; i <= flightSeatingAmount(); i++) {
        System.out.println(nameGenerator() + "," + "0" + "," + airports[airportSelectionOne()]);
    }

You are generating a random number in the method flightSeatingAmount() which could also be zero and then you try to initialize the array of that size. But then you will have to check whether airportSelectionOne() which is also a random assignment, is within the bound of the size or not.

public static void flightManifest() throws FileNotFoundException {

    System.out.println(flightSeatingAmount());
    for (int i = 0; i <= flightSeatingAmount(); i++) {

//GOOD INFORMATION!!!
System.out.println("airportSelectionOne()=" + airportSelectionOne() + ", airports.length=" + airports.length + "");

        System.out.println(nameGenerator() + "," + "0" + "," + airports[airportSelectionOne()]);
    }    
}

That System.out.println I added in the middle of the above function is pretty revealing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM