2017-11-30 2 views
0

저는 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; 
    } 
} 
+2

학교 임무 (솔직하게 말해 주셔서 고맙습니다.)를 감안할 때, 내가 배운 점이 무엇을 시도했는지 철자하는 것이 중요하다고 생각합니다. 이 경우 사용자가 제공 한 추가 승객을로드하려는 시도를 코드에 제공 한 증거가없는 것 같습니다. 당신이 그 고객을 불러들이려고 시도한 것을 제공하십시오. 그렇지 않으면 누군가에게 당신을 위해 숙제를하라고 요구하는 것처럼 보입니다. – Alan

+0

그들은 while 회선에서 Pass [counter] = new Passengers (full_Name, seatClass, seatNumber)와 함께 매번 승객을 만듭니다. @ 앨런? –

답변

0

다음 체크인을하고 있기 때문에 내 생각 엔 당신이 경계 예외 밖으로 배열을 던지고 내 승객 내 코드입니다 비즈니스 및 경제 승객을위한 for 루프

for(int i=0; i<=NUM_OF_PASSENGERS; i++)... 
첫 번째 클래스 승객을위한 그러나

당신은 내가에게 < = NUM_OF_PASSENGERS, 내가 한 번 = 당신이 배열 기반 0이기 때문에 존재하지 않는 인덱스에 액세스를 시도하려고 NUM_OF_PASSENGERS을하고 있기 때문에 당신은

for(int i=0; i<NUM_OF_PASSENGERS; i++)... 

할 배열의 마지막 인덱스는 실제로 NUM_OF_PASSENGERS - 1입니다. 따라서 비즈니스 또는 경제 승객을 인쇄하려고하면 범위를 벗어나는 배열로 실행됩니다.

+0

java.lang.NullPointerException이 Flight_EdgarFlores.main (Flight_EdgarFlores.java:103)에서 \t sun.reflect.NativeMethodAccessorImpl.invoke0 (기본 방법)에서 \t sun.reflect.NativeMethodAccessorImpl.invoke에서 \t (NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke에서 \t (DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke에서 \t (Method.java:606) edu.rice.cs.drjava.model.compiler에서 \t .JavacCompiler.runCommand (JavacCompiler.java:272) **이 오류가 발생합니다 ** –

+0

메인에 103 행은 무엇입니까 ??? @ 에드거 플로어스 그럼 내가 도와 줄 수있어. –

관련 문제