Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 snippet of code that I am working on. I keep getting java.util,InputMismatchException:null (in java.util.Scanner)

In the employer file it is structured like:

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 the mismatch. If anyone could help, that would be amazing.

like image 289
Ethan Avatar asked Jan 30 '26 03:01

Ethan


1 Answers

Your pattern for the scanner is wrong.

The pattern delimiter 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 encounter the last value , 50, and there is no , after that, so there is no match, and the expression fails.

This would be more obvious to you if you put your code 1 statement per line:

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

and then the exception stack trace would point out that the fail happened on the EM line only.

If you change your pattern to match either the comma, or an end-of-line, it works:

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

You should probably be smarter with the pattern than I have been, 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);
    }
}
like image 85
rolfl Avatar answered Jan 31 '26 15:01

rolfl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!