簡體   English   中英

如何停止在我的arrayList上獲取null

[英]How to stop getting null on my arrayList

對於此代碼,我應該讀入一個文件並將其放入ArrayList 我一直獲得ArrayList一半,而另一半則作為我聲明的變量出現。

該代碼應該在另一個擴展的類中運行。

public static void inputDoctorRecords(ArrayList<Person> doc) throws FileNotFoundException {
    //opening file
    Scanner console = new Scanner(new File("Doctor.dat"));

    Doctor dr;
    String lastName = null;
    String firstName = null;
    String Street = null;
    String City = null;
    String State = null;
    String Zip = null;
    int Ident = 0;
    String loc = null;
    double sal = 0.0;
    String Spec = null;
    boolean done = true;
    boolean fin = true;
    String sys = null;

    while(console.hasNext() ){
        String names = console.next();
        dr = new Doctor(lastName, firstName, Street,State, City, Zip, Ident, loc, sal, Spec);

        if (names.equals("LName")){
            lastName = console.next();
        }
        if(names.equals("FName")){
            firstName = console.next();
        }
        if (names.equals("Street")){
            Street = console.nextLine();
        }
        if (names.equals("City")){
            City = console.nextLine();
        }
        if (names.equals("State")){
            State = console.next();
        }
        if (names.equals("Zip")){
            Zip = console.next();
        }
        if (names.equals("Ident")){
            Ident = console.nextInt();               
        }
        if (names.equals("Loc")){ //returns null
            loc = console.next();
        }
        if (names.equals("Salary")){ // returns 0.0
            sal = console.nextDouble();
        }
        if (names.equals("Spec")){ // returns null
            Spec = console.next();
        }
        if (names.equals("next")){ // goes to the next person in file
            doc.remove(dr);
            doc.add(dr);  
        }           
    }
    dr = new Doctor(lastName, firstName, Street,State, City, Zip, Ident, loc, sal, Spec);

    doc.remove(dr);
    doc.add( dr);
    System.out.println( doc);  
}
// here is the input file
            LName   Builder
           FName    Robert
           DOByy    1985
      DOBmm 12
 DOBdd  31
Ident   123456
Loc Seattle
Spec    Cardiology
Salary  100000.0
Street  123 Mockingjay
City    Sector 12
State   WA
Zip 98058
next
LName   Builder
FName   Roberta
DOByy   1988
DOBmm   11
DOBdd   22
Ident   234567
Loc Fife
Spec    Oncology
Salary  120000.0
Street  123 Mockingjay
City    Sector 12
State   WA
Zip 98058
next
LName   Klein
DOByy   1974
DOBdd   06
Loc Tacoma
Street  59 West Rodeo Drive
Ident   345678
City    Hollywood
DOBmm   05
State   CA
FName   Calvin
Zip 90210
Spec    Dermatology
Salary  150000.0
eof

例如,當您說Zip = console.next();時,掃描程序會逐行從InputStreams讀取Zip = console.next(); ,它將丟棄從當前行獲得的所有內容。 我建議使用String#split()拆分字符串。

編輯: Scanner#next()使用該字符串。 那就是我要找的單詞。

試試這個

public static void inputDoctorRecords(ArrayList<Person> doc) throws FileNotFoundException {
    //opening file

    Scanner console = new Scanner(new File("Doctor.dat"));

    Doctor dr;
    String lastName = null;
    String firstName = null;
    String Street = null;
    String City = null;
    String State = null;
    String Zip = null;
    int Ident = 0;
    String loc = null;
    double sal = 0.0;
    String Spec = null;
    boolean done = true;
    boolean fin = true;
    String sys = null;

    while(console.hasNext() ) {
        String names = console.next();

        if (names.equals("LName")) {
            lastName = console.next();
        }
        else if(names.equals("FName")) {
            firstName = console.next();
        }
        else if (names.equals("Street")) { 
            Street = console.nextLine();    
        }
        else if (names.equals("City")) {
            City = console.nextLine();
        }
        else if (names.equals("State")) {
            State = console.next();
        }
        else if (names.equals("Zip")) {
            Zip = console.next();
        }
        else if (names.equals("Ident")) {
            Ident = console.nextInt();               
        }
        else if (names.equals("Loc")) {
            loc = console.next();   
        }
        else if (names.equals("Salary")) {
            sal = console.nextDouble();
        }
        else if (names.equals("Spec")) {
            Spec = console.next();
        }
        else if (names.equals("next")) { // goes to the next person in file
            dr = new Doctor(lastName, firstName, Street,State, City, Zip, Ident, loc, sal, Spec);
            doc.add(dr);      
        }  
        else {
            console.nextLine(); // ignore the line because it is unknown
        }
    }
    // Last Line
    dr = new Doctor(lastName, firstName, Street,State, City, Zip, Ident, loc, sal, Spec);    
    doc.add(dr);
    System.out.println(doc);  
}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM