Reputation: 109
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:00
Today'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:00
Exception 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)
Upvotes: 0
Views: 1460
Reputation: 308763
I'd recommend an Aircraft object instead of separate lists of all those items. Java's an object-oriented language. It's better to encapsulate related attributes into a single class and use that.
public class Aircraft {
private String aircraft;
private String destination;
private Date departureTime;
private int maxPassengers;
// You add the rest.
}
// and this in your main
public Aircraft [] aircrafts = new Aircraft[numAircraft];
But if that's too advanced for you, I'd recommend learning how to read exceptions:
java.lang.ArrayIndexOutOfBoundsException: 4 at AircraftsReport.main(AircraftsReport.java:54)
Open AircraftsReport.java in a text editor and turn on line number display. Go to line 54 - that's where your error is.
This code works. I'd recommend studying it to see why:
import java.util.Scanner;
/**
* AircraftsReport description here
* @author Michael
* @link http://stackoverflow.com/questions/13900443/arrays-program-in-java/13900477#comment19154335_13900477
* @since 12/16/12 6:33 AM
*/
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, 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];
// changed this
for (int ctr = 0; ctr < num2process; ++ctr) {
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;
}
System.out.println("Today's report of international fligts for"+
airline);
System.out.println("\nAIRCRAFTS\tDESTINATION\tNUMBER OF PASSENGERS"+
"\tFLIGHT TIME");
// changed this
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();
}
}
}
Upvotes: 2
Reputation: 69
Well first dont initilize your arrays until you know how many num2process should be. Just looking at that your arrays are a size 0. Ask the num2process and then do your arrays. That will probably fix your problem. Because the loop is going through once and getting 0 back (because you initilized the arrays with 0 before you asked)
Upvotes: 0
Reputation: 101
The Problem with your code is that you are initializing a zero sized array and then asking the user to input num2process. this might do the trick
System.out.print("Enter airline company: ");
airline = input.nextLine();
System.out.print("Enter number of aircrafts to process: ");
num2process = input.nextInt();
// Then initialize your arrays
String[] AIRCRAFTS = new String[num2process];
String[] DESTINATIONS = new String[num2process];
String[] FLIGHT_TIME = new String[num2process];
int[] PASSENGERS_COUNT = new int[num2process];
Upvotes: 0
Reputation: 4035
You are initialsing the value of num2process after you have defined the arrays. The size of the arrays is 0. Modify the code like this.
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];
Upvotes: 0
Reputation: 37813
In this line
int passengersCount = 0, num2process = 0, ctr = 0, ctr2 = 0;
you declare num2process
as 0.
So the next line
String[] AIRCRAFTS = new String[num2process];
creates an array of 0 length.
A few lines after that you reassign num2process
:
num2process = input.nextInt();
but this won't change the size of the previously created array.
You enter the do while
loop once (because they always get executed at least once), and the condition check subsequently fails
} while(ctr < AIRCRAFTS.length);
because ctr
is 1 at this point (after executing ctr++;
) and AIRCRAFTS.length
is still 0.
Upvotes: 2