I am supposed to write a program that could accept user input of aircraft name, destination, number of passengers and flight time. The user is asked if how many aircrafts he/she would like to process. I know that I should make use of arrays. Here’s my current codes but it stops after the input of the first aircraft name.
here’s what’s happening:
Enter airline company: French Air
Enter number of aircrafts to process: 3
Enter aircraft name: ABC
Enter destination: Tokyo
Enter number of passengers: 156
Enter flight time: 10:15
Enter aircraft name: DEF
Enter destination: Chile
Enter number of passengers: 88
Enter flight time: 11:00
Enter aircraft name: FGH
Enter destination: Miami
Enter number of passengers: 157
Enter flight time: 12:00Today’s report of international fligts forFrenchAir
AIRCRAFTS DESTINATION NUMBER OF PASSENGERS FLIGHT TIME
ABC Tokyo 156 10:15
DEF Chile 88 11:00
FGH Miami 157 12:00Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at AircraftsReport.main(AircraftsReport.java:54)
Here my current codes:
import java.util.*;
public class AircraftsReport
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String airline = "";
String strAircraft = "", strDestination = "", strFlightTime = "";
int passengersCount = 0, num2process = 0, ctr = 0, ctr2 = 0;
System.out.print("Enter airline company: ");
airline = input.nextLine();
System.out.print("Enter number of aircrafts to process: ");
num2process = input.nextInt();
String[] AIRCRAFTS = new String[num2process];
String[] DESTINATIONS = new String[num2process];
String[] FLIGHT_TIME = new String[num2process];
int[] PASSENGERS_COUNT = new int[num2process];
while(ctr < num2process)
{
System.out.print("Enter aircraft name: ");
strAircraft = input.next();
AIRCRAFTS[ctr] = strAircraft;
System.out.print("Enter destination: ");
strDestination = input.next();
DESTINATIONS[ctr] = strDestination;
System.out.print("Enter number of passengers: ");
passengersCount = input.nextInt();
PASSENGERS_COUNT[ctr] = passengersCount;
System.out.print("Enter flight time: ");
strFlightTime = input.next();
FLIGHT_TIME[ctr] = strFlightTime;
ctr++;
}
System.out.println("Today's report of international fligts for" +
airline);
System.out.println("\nAIRCRAFTS\tDESTINATION\tNUMBER OF PASSENGERS" +
"\tFLIGHT TIME");
for(ctr2 = 0; ctr2 <= AIRCRAFTS.length; ctr2++)
{
System.out.print(AIRCRAFTS[ctr2] + "\t" + DESTINATIONS[ctr2] +
"\t" + PASSENGERS_COUNT[ctr2] + "\t" + FLIGHT_TIME[ctr2]);
System.out.println();
}
}
}
Please help with figuring out what’s wrong
It produces the output together with the Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at AircraftsReport.main(AircraftsReport.java:54)
In this line
you declare
num2processas 0.So the next line
creates an array of 0 length.
A few lines after that you reassign
num2process:but this won’t change the size of the previously created array.
You enter the
do whileloop once (because they always get executed at least once), and the condition check subsequently failsbecause
ctris 1 at this point (after executingctr++;) andAIRCRAFTS.lengthis still 0.