java - 输入不匹配异常错误

标签 java arrays compiler-errors

我收到输入不匹配错误,但不知道为什么。它正在另一台计算机上工作,但似乎无法在我的笔记本电脑上工作。起初我以为可能是我的txt文件,但这不是原因。

import java.io.*;
import java.util.*;
public class FindGrade {
public static final int NUM_SCORE_TYPES = 5;   

public static void main(String[] args) {
    Scanner scan = null;
    int[] quizArray = null;    
    int[] labArray = null;     
    int[] attendance = null; 
    int[] midterms = null; 
    int quizgrade =0;
    int labgrade=0;
    int attendance_1=0;
    int midterms_1 =0;
    String name;


    try {
        scan = new Scanner(new File("input.txt")); 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }

    // each iteration is for single exam type (ie: Quizzes is the 1st one)
    for (int i = 0; i < NUM_SCORE_TYPES; i++) {

        name = scan.next(); 
        int numScores = scan.nextInt();
        int maxGrade = scan.nextInt();

        if (name.equals("Quizzes")) {
            quizArray = new int[numScores];
            readScores(quizArray, numScores, scan);


        }

        else if (name.equals("Labs")) {
            labArray = new int[numScores];
            readScores(labArray, numScores, scan);

        }
        else if (name.equals("Lab_attendance")) {
            attendance = new int[numScores];
            readScores(attendance, numScores, scan);

        }
        else if (name.equals("Midterms")) {
            midterms = new int[numScores];
            readScores(midterms, numScores, scan);

        }

    }

}


public static void readScores(int[] scoreArray, int numScores, Scanner scan) {
    for (int i = 0; i < numScores; i++) { 
        scoreArray[i] = scan.nextInt();
    }
}

public static void average(double [] scoreArray, int numScores){
    double sum=0;
    for(int i=0; i< scoreArray.length; i++){
        sum += scoreArray[i];
    }
    double average = sum/numScores;

    System.out.println(sum + " " + average);



}

}

输出:
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at FindGrade.main(FindGrade.java:33)

输入文件:
Quizzes 8 10
5 8 9 10 4 0 10 7
Labs 6  100
95  90  100  87  63  92
Lab_attendance  16  1
1  1  1  0  1  1  1  1  0  1  1  1  1  0  1  1
Midterms  2  100
87  94
Final  0  100   

最佳答案

您的代码可以很好地处理您发布的数据。
如果将给变量“名称”分配的值与您期望的5个名称不同,如“测验”而不是“测验”,则会出现此错误。
在这种情况下,您的代码逻辑将被破坏,并且将出现此错误。这是可能的根本原因。

关于java - 输入不匹配异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593920/

相关文章:

java - 今天的日历位于两个日历之间?

java - Rest Web 服务(post)使用 json 不起作用

C++ 指针机制 - 错误 2440

linker - C/C++详细编译错误,多个定义

c++ - 为什么 g++ 在未执行的代码处标记一个转换错误?

java - 如何在 Eclipse/Netbeans IDE for Java NLP 项目中添加 stanford corenlp 库?

Java 编译器无法推断泛型链上的类型

JAVA vector 打印

c++ - 如何在给定间隔内更改数组的值

python多重处理比正常慢-计算太微不足道?