简体   繁体   English

从文本文件读取Java输入不匹配

[英]Java Input Mismatch Reading from Text File

I'm writing a program that will take input from a text file filled with students' grades. 我正在编写一个程序,它将从充满学生成绩的文本文件中获取输入。 These grades get averaged and added to an array list. 这些成绩取平均值并添加到数组列表中。 I'm having trouble getting the file to be read in however. 我在读取文件时遇到了麻烦。 I'm receiving an input mismatch. 我收到输入不匹配的信息。 I've tried changing the loop in my calcAssignmentAverage method to less than 15 (there are 15 lab grades, the 2nd line in the text file), but it still gives an input mismatch. 我尝试将calcAssignmentAverage方法中的循环更改为小于15(有15个实验室等级,即文本文件中的第二行),但是它仍然提供输入不匹配的信息。

File (real file has 16 students in the following format.): 文件(实际文件有16个学生,使用以下格式。):

Puckett, Karen
10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0
4 3 5 3 5 2 3 2 1.5 1 5 3.5
17.5 24 22 23.5 22 23
90 91
96

Code: 码:

 public static void main(String[] args) throws IOException {
    final double NUM_OF_LABS = 15;
    final double NUM_OF_QUIZZES = 12;
    final double NUM_OF_PROJECTS = 6;
    final double NUM_OF_EXAMS = 2;
    final double NUM_OF_FINAL = 1;
    final double LAB_POINTS = 150;
    final double QUIZ_POINTS = 60;
    final double PROJECT_POINTS = 150;
    final double EXAM_POINTS = 200;
    final double FINAL_POINTS = 100;

    //Open input file
    File myFile = new File("scores.txt");
    if (!myFile.exists()) {
        System.out.println("The input file was not found.");
        System.exit(0);
    }

    Scanner inputFile = new Scanner(myFile);

    ArrayList<Student> studentList = new ArrayList<Student>();

    readFile(inputFile, studentList, NUM_OF_LABS, LAB_POINTS, NUM_OF_QUIZZES,
            QUIZ_POINTS, NUM_OF_PROJECTS, PROJECT_POINTS, NUM_OF_EXAMS,
            EXAM_POINTS, NUM_OF_FINAL, FINAL_POINTS);

}

public static void readFile(Scanner inputFile,
        ArrayList<Student> studentList, double NUM_OF_LABS,
        double LAB_POINTS, double NUM_OF_QUIZZES, double QUIZ_POINTS,
        double NUM_OF_PROJECTS, double PROJECT_POINTS,
        double NUM_OF_EXAMS, double EXAM_POINTS, double NUM_OF_FINAL,
        double FINAL_POINTS) {

    //read and set data for student1:
    while (inputFile.hasNext()) {
        //create a new student
        Student currentStudent = new Student();

        //read name
        String name;
        name = inputFile.nextLine();
        currentStudent.setName(name);

        //read lab average 
        double labAverage = 0;
        labAverage = calcAssignmentAverage(NUM_OF_LABS, inputFile, LAB_POINTS);
        currentStudent.setLabAverage(labAverage);


        //add currentStudent to the list
        studentList.add(currentStudent);


    }
}

public static double calcAssignmentAverage(double NUM_ASSIGNMENTS,
        Scanner inputFile, double MAX_POINTS) {

    double sum = 0;
    double average = 0;

    for (int i = 0; i < NUM_ASSIGNMENTS; i++) {
        sum = sum + inputFile.nextDouble();
    }
    average = (sum / MAX_POINTS) * 100;

    return average;
}

Byron is right. 拜伦是对的。 Your logic to advance to the next student is executing prematurely. 您晋升为下一位学生的逻辑过早地执行。 You need to read lines 3 through 6 before you continue to the next iteration of the loop, assuming that they apply to the same student. 假设它们适用于同一学生,则在继续下一个循环之前,您需要阅读第3至6行。

Also, after reading a double, your Scanner will not automatically advance to the next line. 另外,在阅读完一本书之后,扫描仪将不会自动前进到下一行。 You need to do that explicitly. 您需要明确地执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM