for loop - Java index out of bound error -


i having issues index out of bounds in program. loop in method @ bottom supposed run through many times specified in flightseatingamount() method. keep getting error when running program test. 1 or 2 outputs before errors out , other times don't output, error. here 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 airplane program. program go ahead , create list of passengers have gone through 25 different airports.");     system.out.println("the list of airports keep track of listed below\n");      //for listing out airport names     int count = 0;     (int = 0; < 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());     (int = 0; <= flightseatingamount(); i++) {         system.out.println(namegenerator() + "," + "0" + "," + airports[airportselectionone()]);     }  }  } 

here output get:

welcome airplane program. program go ahead , create list of         passengers have gone through 25 different airports. list of airports keep track of 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     @ ole1.flightmanifest(ole1.java:70)     @ ole1.main(ole1.java:33) 

the 843 referring how many times loop supposed execute.

your airportselectionone returned 45. there aren't 46 airports in airports array. means went past end of array, illegal. change function doesn't ever return number higher size of array. hint: can tell size of array airports.length


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -