有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java出现读入文件问题,并将其设置为变量

早上好! 好的,我的代码有问题,我试图读取一个带有数组的文件。但是,问题是,每当我尝试将其设置为变量方法时,就会出现NullPointerException错误。我正在试图阅读一个包含学生全名、gpa和出勤信息的文件。下面是文本文件Week# Y Last First ID Major GPA的一个示例 我应该数几周,Y表示学生在场还是缺席。 反正

我找到了另一种读取文件并设置变量的方法,但当while循环运行时,它只运行一定数量的文本,而不是整个文件,下面是一个例子:

input.nextLine();
String weekNum = input.next();
System.out.print(""+weekNum);//I have this code in a while loop

这是我几个小时来一直在想的另一个代码(完整),有人能帮我吗

        Scanner filechecker = new Scanner("C:/.txt");
        java.io.File file = new java.io.File("C:/.txt");


        Scanner input = new Scanner (file);

        funy [] student = new funy [6] ;


        for (int i=0; i<6; i++){

            input.nextLine();
            String weekNum = input.next();
            student[i].sessionList.add(weekNum);
            System.out.print(""+weekNum);
            String atten = input.next();
            student[i].absentSessionList.add(atten);
            System.out.print("\t "+atten);
            lastName = input.next();
            student[i].setLastName(lastName);
            System.out.print("\t"+lastName);
            firstName = input.next();
            student[i].setFirstName(lastName);
            System.out.print("     "+firstName);
            ID = input.next();
            student[i].setID(ID);
            System.out.print("\t"+ID);
            major = input.next();
            student[i].setMajor(major);
            System.out.print("\t"+major);
            GPA = input.nextDouble();
            student[i].setGPA(GPA);
            System.out.print("\t"+GPA+" ");
            System.out.println("\t");
            input.nextLine();
            input.nextLine();
            input.nextLine();
            input.nextLine();
            student[i].addAttendanceIndicator(weekNum, atten);
        }


        System.out.println(student[0]);//you can ignore these



        System.out.printf("Abs Rate: %.3f\n", student[0].getAbsenteeRate());//this too

    }

}
`

共 (2) 个答案

  1. # 1 楼答案

    你正在使用

    for (int i=0; i<6; i++)
    

    所以它只运行循环中的代码6次。尝试使用

    while(input.hasNextLine()) {...
    

    相反

  2. # 2 楼答案

    你定义了

    funy [] student = new funy [6] ;
    

    但该数组只包含空对象,因此您将在以下位置获得NullPointerException:

    student[i].sessionList.add(weekNum);
    

    正如你在评论中所说的,因为学生[i]对于所有i值都是空的

    您需要创建一个新的funy对象:

    funy yourNewFuny = new funy(...);
    

    然后在循环结束时:

    student[i] = yourNewFuny;
    

    可能是这样的:

    for (int i=0; i<6; i++){  // take care with the index like other answer states
        input.nextLine();
        String weekNum = input.next();
        String atten = input.next();
        // read the rest of the info
    
        funy aFuny = new funy(weekNum, etc); // I don't know how you create funy objects
        student[i] = aFuny;
    }
    

    操作编辑:

    while(input.hasNextLine()){//while loop added
    
                input.nextLine();
                weekNum = input.next();
                //reads in the variables
    
            funy yourNewFuny = new funy();//last line on while loop
    
            for(int i=0; i<6; i++){
            student[i] = yourNewFuny;
            student[i].sessionList.add(weekNum);
    

    它可以工作,但似乎只获得一个学生的信息。但是非常感谢你的帮助!!!你真的帮了我很多