2

I'm currently trying to read a file from my HDD. The file name is "Sample.txt", below is my code. I'm able to get it to compile and run, but receive this error:

 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)

I've tried reading the file w/just a While loop and now with a try/catch, but received the same error, and I'm unsure what's exactly wrong with it. I'm trying to read the file from the Service Class and have the call to the method errorReport() from the Client Class. Any help would be greatly appreciated.

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
4

4 回答 4

1

Considering your file structure below which is assumed from printing statement

  name1 1.1
  name2 2.2
  name3 3.3

Now according to your code following line

 // 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(); 

Now to resolve above issue. You can use 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]);

I hope this helps. If you need anymore help just ask.

于 2013-06-19T21:24:56.733 回答
0

Generally, InputMismatchException is thrown if the thing you're trying to parse doesn't match the format that Scanner expects.

So in this case, check your input file to see if the element you're parsing is actually a double. Be careful too of any extra whitespace.

于 2013-06-19T21:15:54.010 回答
0

This is most likely an issue of your data not matching what your program actually expects. You need to recheck your file structure.

As the stacktrace shows nextDouble, the problem is a non-double within the file where scanner is expecing a double.

于 2013-06-19T21:16:40.000 回答
0

Without knowing what your input file looks like, this error is happening because you are trying to read character data into a double, and those characters being read are not doubles.

Make sure all the data you are reading in is in the format you expect it to be.

If this was me, I would read the entire data into a string, and then try to convert those strings in doubles, so I could surround that statement in a try/catch and then deal with it appropriately.

于 2013-06-19T21:22:18.750 回答