Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 编写向数组添加对象的方法_Java_Arrays_Object_Text Files - Fatal编程技术网

Java 编写向数组添加对象的方法

Java 编写向数组添加对象的方法,java,arrays,object,text-files,Java,Arrays,Object,Text Files,我编写了一个将Student对象添加到花名册数组中的方法 void add(Student newStudent){ int i = 0; while(i != classSize){ //classSize is the size of the roster array if(roster[i] == null { //roster is an array of Student objects roster[i] = newSt

我编写了一个将Student对象添加到花名册数组中的方法

void add(Student newStudent){
    int i = 0;
    while(i != classSize){    //classSize is the size of the roster array
        if(roster[i] == null {   //roster is an array of Student objects
            roster[i] = newStudent;
        }
        i++;
    }
}
我遇到的问题是,当我在主类中使用此方法时,它似乎只添加和打印第一个对象

我的主要方法部分:

ClassRoster firstRoster = new ClassRoster();
scan = new Scanner(inputFile).useDelimiter(",|\\n");
while(scan.hasNext()){
    String name = scan.next();
    int gradeLevel = scan.nextInt();
    int testGrade = scan.nextInt();
    Student newStudent = new Student(name,gradeLevel,testGrade);
    firstRoster.add(newStudent);
    System.out.printf(firstRoster.toString());
}
输入文本文件如下所示:

John,12,95
Mary,11,99
Bob,9,87
但是,当我尝试打印FirstFloster数组时,它只打印第一个对象。在本例中,它将打印John 3次

John,12,95
John,12,95
John,12,95
如果我在文本文件中添加另一个学生,它将只打印John 4次,以此类推

类花名册类中的toString方法:

public String toString(){
    String classString = "";
    for(Student student : roster){
        classString = student.toString();   //The student object uses another toString method in the Student class
    }

    return classString;
}
在这种方法中:

void add(Student newStudent){
    int i = 0;
    while(i != classSize){    //classSize is the size of the roster array
        if(roster[i] == null {   //roster is an array of Student objects
            roster[i] = newStudent;
        }
        i++;
    }
}
将第一个
newStudent
对象分配给数组的所有项
因此,当您尝试分配第二个或第三个项目时,没有一个项目是
null
,也没有完成分配
完成第一次分配后,只需停止循环:

void add(Student newStudent){
    int i = 0;
    while(i != classSize){    //classSize is the size of the roster array
        if(roster[i] == null {   //roster is an array of Student objects
            roster[i] = newStudent;
            break;
        }
        i++;
    }
}
编辑
你的
班级名册
班级的原样只会返回最后一名学生的详细信息
但您还应该检查空值。
因此,请改为:

public String toString(){
    String classString = "";
    for(Student student : roster){
        if (student != null)
          classString += student.toString() + "\n";
    }

    return classString;
}

我不知道你的
学生
类的
toString()
,我假设它按预期工作。

你的
,而
循环用第一个元素填充所有可用位置。然后,由于没有位置是空的,因此不会插入任何内容

循环可以简单地修改为:

void add(Student newStudent){
    int i = 0;
    while(i != classSize){    //classSize is the size of the roster array
        if(roster[i] == null {   //roster is an array of Student objects
            roster[i] = newStudent;
            break;
        }
        i++;
    }
}

现在,一旦填充了一个空位置,程序将退出循环。

将break添加到while循环似乎破坏了我对class花名册类的toString方法。当我尝试使用toString方法时,它会给我一个NullPointerException。我编辑了这篇文章并添加了toString方法。是
toString
方法给出了异常,还是使用
toString
的方法?我认为两者都是线程“main”java.lang.NullPointerException中的异常位于classlotster.toString;在GradeOrganizer.main,你能提供调用
toString
的函数吗?我在主帖子上添加了它,应该在firstLoster.add(newStudent)下的while循环中。将break添加到while循环似乎破坏了我对ClassLoster类的toString方法。当我尝试使用toString方法时,它会给我一个NullPointerException。我编辑了帖子并添加了toString方法。