2017-06-20 108 views
-2

我的任务是创建一个简单的java程序,读取数据文件中的成绩,然后计算以下每组中的成绩等级:[90 - 100] [80 - 89] [70 - 79] [60下面是样本输出:[90-100] 10 [80-89] 9 [70-79] 20 [60-69] 8 [60以下] 2.“线程中的异常”main“java.util.InputMismatchException”是什么意思?

这是什么到目前为止,我有编码:

//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

您正在循环,直到'hasNextInt()'为false。为什么你会期待'nextInt()'事后工作? – shmosel

+1

没有'nextInt()',因为你在第一个'while'循环中耗尽了所有的int。你没有保存任何东西! 'nextInt()'不会神奇地重新开始,当你去你的下一个循环。 – Jyr

+1

谷歌搜索的例外名称,并找到[这个惊人的句子](https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html),它会回答你的问题 - 由扫描仪的_Thrown表明检索到的标记与预期类型的​​模式不匹配,或者标记超出预期类型的​​范围._ – csmckelvey

回答

0

因为你迭代不正确。 这是什么代码的目的?然后再迭代?:

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

     //Read scores 
     input.nextInt(); 
    } 
+1

这很可能是因为他一直在扫描扫描仪,然后在空扫描仪上调用该行。它必须再次实例化。 – DarkCygnus

+0

你说得对,他有一些代码剩菜 – ACV

0

java.util.InputMismatchException是当您使用Scanner接受某种类型(如intString,或char),但不同类型的输入。例如,如果您致电nextInt()并输入String,则将抛出此异常。

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

相关问题