2013-04-18 239 views
1

我正在爲我的java類做我的實驗,並且我這樣做了,但無論如何嘗試修復它,我都不斷收到錯誤。我知道這個問題在我的switch聲明中的case 1之下。我試圖重新排序infiles並添加inFile.nextLine();到列表的開頭,中間,結尾,但這並沒有工作,要麼線程「main」異常中的錯誤java.util.InputMismatchException

這裏是錯誤:

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 Lab6.main(Lab6.java:73) 

這裏是我的代碼:

import java.util.Scanner; 
    import java.io.*; 

    public class Lab6 
    { 
     public static int menu() 
     { 
       int ans; 

       System.out.println("Please chose an option"); 

       System.out.println("1.Class Average"); 
       System.out.println("2.Student Average"); 
       System.out.println("3.Standard Deviation"); 
       System.out.println("4.Letter grades"); 
       System.out.println("5.Minimum/Maximum"); 
       System.out.println("6.Locate : "); 
       System.out.println("7.Locate All students where the difference between exam1 and exam2>15%"); 
       System.out.println("8.Histogram"); 
       System.out.println("9.Update data"); 
       System.out.println("10.Quit"); 

       Scanner keyboard = new Scanner (System.in); 

       ans=keyboard.nextInt(); 

       return ans; 

     } 

     public static void main (String[] args)throws IOException 
     { 
      //variables 
      int choice; 
      int testno; 
      int test1, 
       test2; 
      int NewScore; 
      int count; 
      int sum1, 
       sum2; 
      int avg1, 
       avg2; 
      String name; 



      //get choice 

      Scanner keyboard = new Scanner (System.in); 

      choice=menu(); 

      File myFile = new File("lab6_indata.txt"); 
      Scanner inFile = new Scanner(myFile); 

      switch(choice) 
      { 
       case 1: 
        System.out.println("You Chose Class Average\n"); 
        count=1; 
        sum1=0; 
        sum2=0; 
        avg1=0; 
        avg2=0; 
        while(count<=25) 
        { 
         test1=inFile.nextInt(); 
         test2=inFile.nextInt(); 

         sum1=sum1 + test1; 
         sum2=sum2 + test2; 
         count ++; 
        } 
        avg1 = sum1/count; 
        avg2 = sum2/count; 

        System.out.println("Class Average Test 1:"+ avg1); 
        System.out.println("Class Average Test 2:"+ avg2); 

        break; 

       case 2: 
        System.out.println("You Chose Student Average"); 
        break; 

       case 3: 
        System.out.println("You Chose Standard Deviation"); 
        break; 

       case 4: 
        System.out.println("You Chose Letter grades"); 
        break; 

       case 5: 
        System.out.println("You Chose Minimum/Maximum"); 
        break; 

       case 6: 
        System.out.println("You Chose Locate : "); 
        break; 

       case 7: 
        System.out.println("You Chose Locate All students where the difference between exam1 and exam2>15%"); 
        break; 

       case 8: 
        System.out.println("You Chose Histogram"); 
        break; 

       case 9: 
        System.out.println("You Chose Update data"); 
        System.out.println("Please Enter Name"); 
        name=keyboard.nextLine(); 
        System.out.println("Are you updating test number 1 or 2?"); 
        testno=keyboard.nextInt(); 
        System.out.println("What is the new score?"); 
        NewScore=keyboard.nextInt(); 
        break; 

       case 10: 
        System.out.println("Program Ended"); 
        break; 

       default: 
        System.out.println("Error: Invaild option"); 
        break; 

      } 

      inFile.close(); 



     } 


} 

這是我INFILE:

*Robert  70 82 
*Joel  64 50 
*Alice  98 95 
*Jasmine 76 92 
*Larry  42 69 
*Elizabeth 79 85 
*Frank  75 66 
*Christine 20 36 
*Alex  0 52 
*Ariel  84 81 
*Luis  65 77 
*Nicole  40 89 
*Mitch  90 94 
*Randy  88 86 
*Tammy  91 84 
*Sarah  78 71 
*Samuel  80 66 
*Lauren  55 63 
*Deanna  97 99 
*Mathew  100 87 
*Justin  68 76 
*Beth  96 95 
*Rebecca 85 83 
*Paul  44 65 
*Lucy  34 56 

也許我INFILE需要是形式有不同的意思?

+1

使用一個調試器,或者在每個nextInt()附近捕獲異常,它會告訴你哪一個問題會導致問題,這會指向正確的方向。 – John3136

回答

0

中的每一行的第一個標記是一個String等都需要任何整數可以讀取之前

String studentName = inFile.next(); 
test1 = ... 
+0

工作。感謝:D – XanderXIV

0

javadocs要消耗:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

機會是你的輸入格式不正確和java沒有比較兩種不同格式的東西,比如intstring

相關問題