什么“线程中的异常”主“java.util.InputMismatchException”是什么意思?

时间:2017-06-20 23:30:31

标签: java exception io

我的任务是创建一个简单的java程序,读取数据文件中的成绩,然后计算以下每组中的成绩:[90 - 100] [80 - 89] [70 - 79] [ 60 - 69] [60以下]以下是样本输出:[90 - 100] 10 [80 - 89] 9 [70 - 79] 20 [60 - 69] 8 [60以下] 2。

这是我到目前为止编码的内容:

//Import the needed header files
import java.util.*;
import java.io.*;


//Class
public class GradeDistribution
{
public static void main(String[] args) throws FileNotFoundException{
  //Sets up varaibles and assign string numbers to int values
  Scanner input=new Scanner(new File(args[0])); 

    //Declare the needed varaibles and initialize them
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int sCount = 0;



    //Loop to read till the "data.txt" has scores
    while(input.hasNextInt())
    {
        //Increment score counter
        sCount++;

        //Read scores
        input.nextInt();
    }



    //Score array
    int score[] = new int[sCount];

    //Loop to iterate through the scores
    for(int i=0;i<sCount;i++)
    {
        //Read
        score[i]=input.nextInt();

        //Check condition for [90-100]
        if (score[i]<=100 && score[i]>=90)
        {
            //Increment count1
            count1 = count1 + 1;               
        }

        //Check condition for [80-89]
        else if (score[i]<90 && score[i]>=80)
        {
            //Increment count2
            count2 = count2 + 1;               
        }

        //Check condition for [70-79]
        else if (score[i]<80 && score[i]>=70)
        {
            //Increment count3
            count3 = count3 + 1;               
        }

        //Check condition for [60-69]
        else if (score[i]<70 && score[i]>=60)
        {
            //Increment count4
            count4 = count4 + 1;               
        }   

        //Otherwise below 60
        else
        {
            //Increment count5
            count5 = count5 + 1;               
        }                       
    }

    //Print
    System.out.println("[90-100] "+count1);
    System.out.println("[80-89] "+count2);
    System.out.println("[70-79] "+count3);   
    System.out.println("[60-69] "+count4);       
    System.out.println("[below 60] "+count5);       
}
 }

代码看起来并且逻辑上是可执行的,但是当我运行它时我收到了这个错误,

Exception in thread "main" java.util.InputMismatchException 
 at java.util.Scanner.throwFor(Unknown Source) 
 at java.util.Scanner.next(Unknown Source) 
 at java.util.Scanner.nextInt(Unknown Source) 
 at java.util.Scanner.nextInt(Unknown Source) 
 at GradeDistribution.main(GradeDistribution.java:43)

为什么会这样?

2 个答案:

答案 0 :(得分:0)

因为您错误地迭代。 这段代码的目的是什么?然后再次迭代?:

  //Loop to read till the "data.txt" has scores
    while(input.hasNextInt())
    {
        //Increment score counter
        sCount++;

        //Read scores
        input.nextInt();
    }

答案 1 :(得分:0)

java.util.InputMismatchException是指您使用Scanner接受特定类型(例如intStringchar),但使用的是其他类型输入。例如,如果您调用nextInt()并输入String,则会抛出此异常。

您的代码中的问题可能在您的第一个while循环中,您可以在其中调用input.nextInt()。这只检查整数,而你的文件可能包含一个String。