2013-06-19 57 views
2

我目前正試圖從我的硬盤讀取文件。文件名是「Sample.txt」,下面是我的代碼。我能夠得到它的編譯和運行,但收到​​此錯誤:java.util.InputMismatchException輸出錯誤

Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Scanner.java:909) 
at java.util.Scanner.next(Scanner.java:1530) 
at java.util.Scanner.nextDouble(Scanner.java:2456) 
at Proj1GradesService.errorReport(Proj1GradesService.java:42) 
at Proj1GradesClient.main(Proj1GradesClient.java:13) 

我試圖用一個try/catch讀取文件瓦特/只While循環而如今,卻收到了同樣的錯誤,我不確定它到底有什麼問題。我試圖從服務類讀取文件,並從客戶端類調用errorReport()方法。 任何幫助將不勝感激。

import java.util.*; //allows use of Scanner class 
import java.io.*; //for File and IOException classes 

class Proj1GradesService 
{ //begin Proj1GradesService 

    public void pageAndColHeading(char letter) //accepts char as a parameter 
    { //start pageAndColHeading 
    switch (letter) 
    { //start switch 
     case 'e': //write the caption for error report 
      System.out.println ("Error Report - Students With Invalid GPA"); //prints Error Report 
      break; 

     case 'v': //write the caption for valid report 
      System.out.println ("Valid Report - Students With Valid"); //prints Valid Report 
      break; 
     default: ; //do nothing 
    }//end switch 
    } //end pageAndColHeading 

    public void errorReport() throws IOException 
    { //start errorReport 

    Scanner scanFile = null; 
    try 
    { 
     scanFile = new Scanner (new File ("p1SampleGPAData.txt")); 
    } 
     catch (FileNotFoundException fnfe) 
     { 
      System.out.println ("wrong file name."); 
     } 

    String name; //name read from file 
    double gpa; //gpa read from file 
    int count = 0; //line # 

    while (scanFile.hasNext()) 
    { 
     name = scanFile.next(); 
     gpa = scanFile.nextDouble(); 
     System.out.println ("Line Number: " + count + "Name: " + name + "GPA: " + gpa); 

     ++count; 
    } //end while 
    scanFile.close(); 

    } //end errorReport 


} //end class 
+0

你能提供一個文本文件的樣本,所以我們可以看到你想要解析什麼格式? –

回答

1

考慮低於你的文件結構現在根據你的代碼下面的行

// consume your whole line. ie name1 1.1 
name = scanFile.next(); 
// looking for double but instead getting string ie name2 
// hence throwing InputMismatchException 
gpa = scanFile.nextDouble(); 

我們解決上述問題,從打印語句

name1 1.1 
    name2 2.2 
    name3 3.3 

假設。您可以使用String.split()

// collect whole line 
    name = scanFile.next(); 

    // split by one or more whitespace OR use your delimiter 
    String[] str = name.split("\\s+"); 

    // gives name 
    String actName = str[0]; 

    // gives gpa, throws NumberFormatException if str[1] is not double value 
    double gpa = Double.parseDouble(str[1]); 

我希望這會有所幫助。如果你需要更多的幫助,請問。

+0

謝謝你的協助。我改變了代碼。我也沒有意識到該文件有2個字符串和1個雙精讀,firstName,lastName和gpa。因此,部分失配錯誤來自於嘗試將lastName讀爲double,所以我也修復了這部分。 –

0

一般來說,如果你試圖解析東西不格式匹配Scanner預計InputMismatchException被拋出。

所以在這種情況下,檢查你的輸入文件,看看你解析的元素是否實際上是double。還要小心任何額外的空格。

0

這很可能是您的數據不符合您的程序實際預期的問題。 您需要重新檢查您的文件結構。

由於堆棧跟蹤顯示nextDouble,問題是scanner指定爲double的文件內的非雙。

0

不知道你的輸入文件是什麼樣子,這個錯誤發生是因爲你正在嘗試將字符數據讀入double,並且那些被讀取的字符不是雙精度。

確保您閱讀的所有數據都採用您期望的格式。

如果這是我,我會將整個數據讀入一個字符串,然後嘗試將這些字符串轉換爲雙精度型,這樣我就可以圍繞try/catch中的該語句,然後對其進行適當的處​​理。