繁体   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