Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何停止在我的arrayList上获取null_Java - Fatal编程技术网

Java 如何停止在我的arrayList上获取null

Java 如何停止在我的arrayList上获取null,java,Java,对于这段代码,我应该读入一个文件并将其放入ArrayList。我不断得到我的ArrayList的一半,而另一半则是我声明的变量 代码应该通过另一个类运行,该类是扩展的 public static void inputDoctorRecords(ArrayList<Person> doc) throws FileNotFoundException { //opening file Scanner console = new Scanner(new File("Docto

对于这段代码,我应该读入一个文件并将其放入
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);  
}

当您说例如
Zip=console.next()时,扫描仪逐行读取InputStreams,它将丢弃从当前行获得的所有内容。我建议使用String#split()拆分字符串

EDIT: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);  
}
publicstaticvoidinputdoctorrecords(arraylistdoc)抛出FileNotFoundException{
//打开文件
扫描仪控制台=新扫描仪(新文件(“Doctor.dat”);
博士;
字符串lastName=null;
字符串firstName=null;
String Street=null;
字符串City=null;
字符串状态=null;
字符串Zip=null;
int Ident=0;
字符串loc=null;
双sal=0.0;
字符串Spec=null;
布尔完成=真;
布尔值=真;
字符串sys=null;
while(console.hasNext()){
字符串名称=console.next();
if(name.equals(“LName”)){
lastName=console.next();
}
else if(name.equals(“FName”)){
firstName=console.next();
}
else如果(names.equals(“Street”){
Street=console.nextLine();
}
else if(name.equals(“城市”)){
City=console.nextLine();
}
else if(name.equals(“State”)){
State=console.next();
}
else if(name.equals(“Zip”)){
Zip=console.next();
}
else if(name.equals(“Ident”)){
Ident=console.nextInt();
}
else if(名称等于(“Loc”)){
loc=console.next();
}
else if(姓名等于(“工资”)){
sal=控制台.nextDouble();
}
else if(名称等于(“规格”)){
Spec=console.next();
}
else if(names.equals(“next”){//转到文件中的下一个人
dr=新医生(姓、名、街道、州、市、邮编、识别号、loc、sal、Spec);
文件添加(dr);
}  
否则{
console.nextLine();//忽略该行,因为它是未知的
}
}
//最后一行
dr=新医生(姓、名、街道、州、市、邮编、识别号、loc、sal、Spec);
文件添加(dr);
系统输出打印项次(doc);
}

希望这有帮助

您不会显示在哪里声明和初始化列表,所以我猜。。。初始化它。另外:提高代码的效率。例如,如果您有:if(names.equals(“Salary”){sal=console.nextDouble();}if(names.equals(“Spec”){Spec=console.next();}替换为:if(names.equals(“Salary”){sal=console.nextDouble();}else if(names.equals(“Spec”){Spec Spec=console.next();}因为不可能两者都是真的。此外,如果您不了解结果,请检查这些方法的作用。还请提供一个示例输入文件,这有助于更好地理解问题……问题是,如果
hasNext
,您必须在每次检查之前进行检查。这意味着您应该使用
开关更改所有
if
,或者
if
/
else
在循环的每次迭代中创建一个新的
医生。那真的没有必要。您只需在调用
remove
/
add
之前创建
Doctor
。“扔掉”11个
Doctor
s中的10个会降低程序的性能。这不会重复信息吗?如果第一位医生给出他的名字,而其他医生没有,那么所有的医生都会有相同的名字?谢谢!但是我得到了错误:线程“main”java.util.NoSuchElementException中的异常:在Clinic.inputDoctorRecords(Clinic.java:185)的Clinic.main(Clinic.java:34)的java.util.Scanner.nextLine(Scanner.java:1540)中找不到任何行。我没有所有的代码来知道是哪一行导致了异常。您知道是哪一行导致了异常吗?尝试删除
else{console.nextLine();}
如果这回答了您的问题,请接受答案。如果没有,请更新您的问题。