InputMismatchException: null

    int EID, ESSN, EM, FSSN, FM;
    String EN, EEN, FN;
    Scanner Ein = new Scanner(employer);
    Scanner Fin = new Scanner(filers);
    Ein.useDelimiter(", *");
    Fin.useDelimiter(", *");
    ArrayList<Employer> EmployIn = new ArrayList<Employer>();
    ArrayList<Filer> FilerIn = new ArrayList<Filer>();
    while (Ein.hasNextLine()) 
   {
          EN = Ein.next(); EID = Ein.nextInt(); EEN = Ein.next(); ESSN = Ein.nextInt(); EM = Ein.nextInt();
          EmployIn.add(new Employer(EN, EID,EEN,ESSN,EM));

    }

      

Here is a piece of code I am working on. I keep getting java.util, InputMismatchException: null (in java.util.Scanner)

In the employer's file, it is structured as follows:

Google, 0, BOX CHARLES, 724113610, 50
Microsoft, 2, YOUNG THOM, 813068590, 50
Amazon, 4, MCGUIRE MARK, 309582302, 50
Facebook, 8, MOFFITT DON, 206516583, 50

      

I have no idea why I am getting a mismatch. If anyone could help it would be amazing.

+3


source to share


1 answer


Your scanner template is incorrect.

The pattern separator is ", *", which is interpreted as a regular expression consisting of a comma followed by any number of spaces.

At the end of the line, you have encountered the last value , 50

and there is no after that ,

, so there is no match and the expression does not work.

It would be more obvious to you if you put your code 1 on line:

EN = Ein.next();
EID = Ein.nextInt();
EEN = Ein.next();
ESSN = Ein.nextInt();
EM = Ein.nextInt();

      



and then the exception stack trace will indicate that the failure occurred only on the EM line.

If you change your template to match comma or end of line, it works:

Ein.useDelimiter(Pattern.compile("(, *| *$)", Pattern.MULTILINE));

      

You should probably be smarter with the template than I am, but the following code works for me:

public static void main(String[] args) throws FileNotFoundException {
    int EID, ESSN, EM, FSSN, FM;
    String EN, EEN, FN;
    Scanner Ein = new Scanner(new File("employers.txt"));
    Ein.useDelimiter(Pattern.compile("(, *| *$)", Pattern.MULTILINE));
    while (Ein.hasNextLine()) {
        EN = Ein.next();
        EID = Ein.nextInt();
        EEN = Ein.next();
        ESSN = Ein.nextInt();
        EM = Ein.nextInt();
        System.out.printf("%s %d %s %d %d %n", EN, EID, EEN, ESSN, EM);
    }
}

      

+2


source







All Articles