java.util.InputMismatchException输出错误
我目前正试图从我的硬盘读取一个文件。文件名是“Sample.txt”,下面是我的代码。我可以让它编译并运行,但收到以下错误:java.util.InputMismatchException输出错误,java,file-io,inputmismatchexception,Java,File Io,Inputmismatchexception,我目前正试图从我的硬盘读取一个文件。文件名是“Sample.txt”,下面是我的代码。我可以让它编译并运行,但收到以下错误: 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(S
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)
我试着用While循环读取文件,现在使用try/catch,但收到了相同的错误,我不确定它到底出了什么问题。我试图从服务类读取文件,并从客户机类调用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
通常,如果您试图解析的内容与
Scanner
期望的格式不匹配,则会抛出inputmaschException
因此,在本例中,请检查您的输入文件,以查看您正在解析的元素是否实际上是一个
double
。也要小心任何额外的空白。这很可能是因为您的数据与程序实际期望的不匹配。
您需要重新检查文件结构
正如stacktrace显示的
nextDouble
,问题在于文件中的扫描器
正在执行双精度扫描时出现非双精度扫描。在不知道输入文件外观的情况下,发生此错误是因为您试图将字符数据读取到双精度扫描中,而正在读取的字符不是双精度扫描
确保您正在读取的所有数据都是您期望的格式
如果是我,我会将整个数据读入一个字符串,然后尝试将这些字符串转换成双倍,这样我就可以将该语句包围在try/catch
中,然后进行适当的处理 考虑到下面的文件结构,它是从打印语句中假定的
name1 1.1
name2 2.2
name3 3.3
现在根据下面的代码行
// 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();
现在来解决上述问题。你可以用
我希望这有帮助。如果您需要更多帮助,请询问。您能否提供文本文件的示例,以便我们了解您试图解析的格式?谢谢您的帮助。我改了密码。我也没有意识到这个文件有两个字符串和一个要读取的双精度字符串:firstName、lastName和gpa。因此,部分不匹配错误来自尝试将lastName读取为double,因此我也修复了该部分。
// 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]);