我正在为CSCI课做功课,并且已经遇到了障碍。
在我们的作业中,我们创建了一个乘客类,然后是我们的飞行代码。
我们的飞行是从文本文件中读取前20个人,然后将它们分配到我们的数组对象中的某个位置。在输入这20个人之后,其余乘客(21-55)将由用户插入对话框。
我已经完成了所有这些,但在此之后我们需要打印出我们阵列中的每个人,这取决于他们所拥有的飞行课程,即所有头等舱的人都打印出来,然后是商业和经济。当我尝试打印所有对象时,只打印文本文件中的人,而不是用户输入的所有人。
这是我的代码
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Scanner;
public class Flight_EdgarFlores
{
public static void main(String[] args) throws IOException
{
String fullName=null;
String seatClass=null;
String seatNumber=null;
String ff_Number=null;
String points=null;
String line, word;
int seat_Choice, again;
Passengers[] Pass;
final int NUM_OF_PASSENGERS= 55; //size of array
Pass = new Passengers[NUM_OF_PASSENGERS];
Scanner lineScan;
Scanner fileScan = new Scanner(new File("PassengerList.txt"));
int line_counter = 0;
int word_counter = 0;
while(fileScan.hasNext())
{
line = fileScan.nextLine();
lineScan = new Scanner(line); // Scans each line of the file
lineScan.useDelimiter(", ");
word_counter = 0;
while(lineScan.hasNext()) // Reads each word in the line
{
word = lineScan.next();
switch (word_counter)
{
case 0:
fullName = word;
break;
case 1:
seatClass = word;
break;
case 2:
seatNumber = word;
break;
case 3:
ff_Number = word;
break;
case 4:
points = word;
break;
}
word_counter++;
}
if(word_counter==3)
Pass[line_counter] = new Passengers(fullName, seatClass, seatNumber);
if(word_counter==5)
Pass[line_counter] = new Passengers(fullName, seatClass, seatNumber, ff_Number, points);
line_counter++;
}
int counter=20;
do
{
String full_Name = JOptionPane.showInputDialog("Enter Full Name: ");
String[] seatClassChoice = new String[3];
seatClassChoice[2] = "Economy Class";
seatClassChoice[1] = "Business Class";
seatClassChoice[0] = "First Class";
int classChoice = JOptionPane.showOptionDialog(null, "Choose Seat Class", "Seat Class Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatClassChoice, seatClassChoice[0]);
if(classChoice==0)
{
String[] seatChoice = {"1A", "2A", "3A", "4A", "5A", "6A"};
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
else if(classChoice==1)
{
String[] seatChoice = {"7A", "7B", "8A", "8B", "9A", "9B", "10A", "10B", "11A", "11B"};
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
else if(classChoice==2)
{
String[] seatChoice = {"12A", "12B", "12C", "13A", "13B", "13C", "14A", "14B", "14C", "15A", "15B", "15C", "16A", "16B", "16C", "17A", "17B", "17C", "18A", "18B", "18C", "19A", "19B", "19C", "20A", "20B", "20C", "21A", "21B", "21C", "22A", "22B", "22C", "23A", "23B", "23C", "24A", "24B", "24C" };
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
Pass[counter] = new Passengers(full_Name, seatClass, seatNumber);
again = JOptionPane.showConfirmDialog(null, "Add another Passenger?");
counter++;
}while((counter<NUM_OF_PASSENGERS)&& (again==JOptionPane.YES_OPTION));
Pass[0].addFrequentFlyer("#2AZ433", "4455"); //adds flyer number and flyer points to object at index 0
for(int i=0; i<NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("First Class"))
System.out.println(Pass[i] + " , " + i);
}
for(int i=0; i<=NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("Business Class"))
System.out.println(Pass[i] + " , " + i);
}
for(int i=0; i<=NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("Economy Class"))
System.out.println(Pass[i] + " , " + i);
}
}
}
这是我的乘客舱
public class Passengers
{
private String fullName, seatClass, seatNumber, frequentFlyerNumber, flyerPoints;
public Passengers (String full_name, String seat_class, String seat_number, String frequent_flyernumber, String flyer_points)
{
fullName=full_name;
seatClass=seat_class;
seatNumber=seat_number;
frequentFlyerNumber=frequent_flyernumber;
flyerPoints=flyer_points;
}
public Passengers (String full_name, String seat_class, String seat_number)
{
fullName=full_name;
seatClass=seat_class;
seatNumber=seat_number;
}
public void addFrequentFlyer (String frequent_flyer_number, String flyer_Points)
{
frequentFlyerNumber=frequent_flyer_number;
flyerPoints=flyer_Points;
}
/* public String getLastName()
{
String lastName="";
int nameLength= fullName.length();
for(int i =nameLength; i> 0; i--)
{
if(full_name.charAt(i)!=' ')
lastName+=fullName.charAt(i);
else
break;
}
}*/
public String getseatClass()
{
return seatClass;
}
public String toString()
{
return fullName + ", "+ seatClass + ", " + seatNumber;
}
}
答案 0 :(得分:0)
我的猜测是你抛出一个超出界限的例外,因为你正在为商务和经济乘客进行以下检查
Gtk.ResponseType
然而,对于您的头等舱乘客,
for(int i=0; i<=NUM_OF_PASSENGERS; i++)...
由于您正在执行i&lt; = NUM_OF_PASSENGERS,一旦i = NUM_OF_PASSENGERS,您将尝试访问不存在的索引,因为数组基于0且数组中的最后一个索引实际上是NUM_OF_PASSENGERS - 1,因此当您尝试打印商务或经济舱乘客时,您会遇到一个超出界限的阵列。