Search code examples
javaregexiojava.util.scannerinputmismatchexception

Java Scanner Throws InputMismatchException even when next token appears to fit data type


I'm currently working with the Java Scanner class and I'm getting a very strange InputMismatchException. I initialize the scanner like this:

Scanner in = new Scanner(new File(fileName));

in.useDelimiter("\n?.*:");

When I call System.out.println(in.next());, it prints 1500 to the command line, which is the correct value. However, if I call System.out.println(in.nextInt()); the Scanner throws an InputMismatchException. I don't think I'm printing trailing newlines or whitespace, and I'm sort of lost as to why this is happening.

EDIT: Here's the data format for what I'm reading in:

Data Description:100 Next Data Description:200

EDIT: Calling Integer.parseInt(in.next()); results in a NumberFormatException: For input string: "1"00.


Solution

  • That InputMismatchException isn't strange at all, because your regex isn't cross-platform compatible. You're likely a Windows user, so your text file will have \r\n as it's line endings, not just \n.

    Thus the proper pattern would be:

    in.useDelimiter("\r?\n?.*:");
    

    To accept \r\n or a single \n.

    You could also try to work with System.lineSeparator() to support different line endings, but then you would need to wrap that with parenthese so the regex quantifier ? applies to both chars \r\n for Windows systems:

    in.useDelimiter(String.format("(%s)?.*:", System.lineSeparator()));
    

    The issue with that approach is that it fails when using Windows as the operating system, but receiving the file from somewhere else, which itself just uses \n. So I recommend the first approach.