2013-06-19 60 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中的该语句,然后对其进行适当的处​​理。