Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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
使用jfilechooser for java将文本文件放入数组_Java_Io_Inputstream_Jfilechooser - Fatal编程技术网

使用jfilechooser for java将文本文件放入数组

使用jfilechooser for java将文本文件放入数组,java,io,inputstream,jfilechooser,Java,Io,Inputstream,Jfilechooser,我一直将数组设置为出界,因为学生可能有不同的分数,因此,如果一个学生没有3年级,但另一个学生有,我的程序会自动崩溃。我该怎么做才能使课程根据学生的不同而有不同的分数 public class StudentList extends Component{ static ArrayList<Student> studentList = new ArrayList<>(); public static final int ARRAYMAX=4; public void

我一直将数组设置为出界,因为学生可能有不同的分数,因此,如果一个学生没有3年级,但另一个学生有,我的程序会自动崩溃。我该怎么做才能使课程根据学生的不同而有不同的分数

public class StudentList  extends Component{

static  ArrayList<Student> studentList = new ArrayList<>();
public  static final int ARRAYMAX=4;

public void readStudent()throws Exception{

    File window = new File(System.getProperty("user.home"));
    JFileChooser choice = new JFileChooser();
    choice.setCurrentDirectory(window);
    int option = choice.showOpenDialog(this);
    File selectedFile = choice.getSelectedFile();
    if (option == JFileChooser.APPROVE_OPTION) {
        System.out.println("Selected file: " + selectedFile.getAbsolutePath());
    }
    File studentFile = new File(selectedFile.getAbsolutePath());

    Scanner in = new Scanner(studentFile);

     while (in.hasNext()) {
         String data = in.nextLine();

            String[] studentData = new String[ARRAYMAX];
            studentData = data.split("\\|");

                for(int i =0; i<ARRAYMAX; i++){
                    studentData[i] ="0";
               }

        String firstName = studentData[0];
        String lastName  = studentData[1];

        double grade1 = Double.parseDouble(studentData[2]);
        double grade2 = Double.parseDouble(studentData[3]);

        double grade3 = Double.parseDouble(studentData[4]);

        Student newStudent = new Student(firstName,lastName);
        newStudent.setGrades1(grade1);
        newStudent.setGrades2(grade2);
        newStudent.setGrades3(grade3);
     }

  }
公共类学生列表扩展组件{
静态ArrayList studentList=新建ArrayList();
公共静态最终int-ARRAYMAX=4;
public void readStudent()引发异常{
文件窗口=新文件(System.getProperty(“user.home”);
JFileChooser choice=新建JFileChooser();
choice.setCurrentDirectory(窗口);
int option=choice.showOpenDialog(此选项);
File selectedFile=choice.getSelectedFile();
if(option==JFileChooser.APPROVE\u选项){
System.out.println(“所选文件:+selectedFile.getAbsolutePath());
}
File studentFile=新文件(selectedFile.getAbsolutePath());
扫描仪输入=新扫描仪(studentFile);
while(在.hasNext()中){
字符串数据=in.nextLine();
String[]studentData=新字符串[ARRAYMAX];
studentData=data.split(“\\\\”);
将(int i=0;i默认成绩(1级、2级等)设置为合理值。在访问和设置成绩之前,请检查studentData的长度(
studentData.length


另一方面,为什么要使用单独的
grade
field/setter?只需将所有成绩存储在列表或数组字段中,然后根据需要访问它。

好吧,如果我理解正确,有些行没有三级。如果是这样,您可以检查它是否有三级
公共类StudentList扩展组件{static ArrayList studentList=new ArrayList();public static final int ARRAYMAX=4;public void readStudent()引发异常{File window=new File(System.getProperty(“user.home”);JFileChooser choice=new JFileChooser();choice.setCurrentDirectory(window);int option=choice.showOpenDialog(this);File selectedFile=choice.getSelectedFile();如果(option==JFileChooser.APPROVE_option){System.out.println(“所选文件:+selectedFile.getAbsolutePath());}file studentFile=new file(selectedFile.getAbsolutePath());Scanner in=new Scanner(studentFile);而(in.hasNext()){String data=in.nextLine();String[]studentData=new String[ARRAYMAX];studentData=data.split(\\\\\\\;);for(int i=0;istudentData的长度等于ARRAYMAX,即4,但您正在尝试访问studentData[4],这是数组中的第5个索引。这将不起作用,必须将ARRAYMAX更改为5才能访问索引4。数组的索引为0。