2014-11-23 68 views
0

我目前正在使用繼承,正如您在下面看到的,我有兩個類,即人員和乘客。在我有主toString()方法的人中,在乘客中,我調用super.toString()並添加新信息,優先預訂/ noOfBags和ID。我遇到的問題是,當用戶將新乘客添加到陣列中時,當您打印陣列時,數據的格式與輸入的格式不同。我很確定,我已經正確地調用了所有東西,我不是100%確定爲什麼只是toString的名稱方面出現混亂(請注意,DOB/ID/noOfBags和優先級預定的所有格式都正確)。如果有人能指出我爲什麼只是名稱打印不正確,將不勝感激。將數據讀入數組時的格式化問題

代碼 這是主類人

public Person() 
{ 
    name = new Name(); 
    dateOfBirth = new Date(); 

} 

public Person(String titleIn, String firstNameIn, String surNameIn, int day, int month, int year) 
{ 
    name = new Name(titleIn, firstNameIn, surNameIn); 
    dateOfBirth = new Date(day, month, year); 

} 

這裏的構造是我使用以獲取新的乘客的姓名和出生日期的細節

public void read() 
{ 
    try 
    { 
     Scanner kbInt = new Scanner(System.in); 
     Scanner kbString = new Scanner(System.in); 

     System.out.println("***Passenger Details: ***"); 
     System.out.print("Title : ");titleRead=kbInt.nextLine(); 
     System.out.print("First Name : ");FNameRead=kbString.nextLine(); 
     System.out.print("Surname : ");SNameRead=kbString.nextLine(); 
     name = new Name(titleRead, FNameRead, SNameRead); 

     System.out.println("\n"); 
     System.out.println("***Date of Birth: ***"); 
     System.out.print("Day : ");dayRead=kbInt.nextInt(); 
     System.out.print("Month : ");monthRead=kbInt.nextInt(); 
     System.out.print("Year : ");yearRead=kbString.nextInt(); 
     dateOfBirth = new Date(dayRead, monthRead, yearRead); 
    } 

     catch (InputMismatchException e) 
     { 
      System.out.print("Incorrect input, please input data in the correct format!"); 
     } 
} 

最後的方法對於人類,這裏是toString

public String toString() 
{ 
    String nameAndAge = "Name = " + name + ", DOB = " + dateOfBirth; 
    return nameAndAge; 
} 

在乘客類繼承Person,這裏是正在取得的ArrayList和構造

private ArrayList<Passenger> passengers = new ArrayList<Passenger>(); 

//Constructor 
public Passenger() 
{ 
    noOfBags = 0; 
    priorityBoarding = false; 
    id = nextid++; 
} 

//Initiliaze constructor 
public Passenger(String titleIn, String firstNameIn, String surnameIn, int day, int month, int year, int bag, boolean pb) 
{ 
    super(titleIn, firstNameIn, surnameIn, day, month, year); 
    noOfBags = bag; 
    priorityBoarding = pb; 
} 

在這裏,我添加noOfBags和優先登機的閱讀和它的人

添加到讀法
public void bagsPriorityRead() 
{ 
    Scanner kbInt = new Scanner(System.in); 
    Scanner kbString = new Scanner(System.in); 

    super.read(); 
    System.out.print("Number of bags : ");bagsRead=kbString.nextInt(); 
    System.out.print("Priority boarding (Y/N) : ");priorityRead=kbString.next().charAt(0); 
} 

最後,這裏是旅客toString方法

//ToString Method 
public String toString() 
{ 
    return " ID: " +id + " - " + super.toString() + " \tNo of Bags: " +bagsRead + "\tDo they have priority boarding? : " +priorityRead; 
} 

在法案中加入的//添加圖片離子和打印

adding the passenger

//打印 Improperly formatted

當如上所述,如果在那裏我去錯了任何人都可以指向不正確的格式/如何解決我的錯誤時,它的格式不正確的方式這將不勝感激。如果需要更多的代碼來找到問題的根源,請讓我知道。

在此先感謝,傑森

+0

是你問的錯誤:標題:Doe Firstname先生姓氏約翰壞了?你爲什麼要分配'name = new Name(titleRead,FNameRead,SNameRead);'等等,而不是調用你的Passenger構造函數? – CreationEdge 2014-11-23 02:28:01

回答

1

貌似問題,如果任何地方,將是姓名::的toString()或參數的順序是錯誤的new Name(x, y, z)

String nameAndAge = "Name = " + name + ", DOB = " + dateOfBirth; 

但你還沒有發佈Name類。

+0

道歉,愚蠢的是這是錯誤,我甚至沒有想到我的名字班是錯誤的順序,感謝它引起了我的注意。 – JasonL95 2014-11-23 02:31:25