3
    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.

rolfl
  • 17,539
  • 7
  • 42
  • 76
Ethan
  • 31
  • 2

1 Answers1

2

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);
    }
}
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • @rofl I changed it and used a different list and now I am getting a NoSuchElementException on that line BLACKROCK, 18, LYNUM WILLIAM, 486576979, 115 ANADARKO PETROLEUM, 4, LYNUM WILLIAM, 486576979, 89 AUTOLIV, 44, ESQUILIN LINDA, 458537755, 80 – Ethan Oct 02 '14 at 01:05