Search code examples
javaintmismatch

Why does this throw a InputMismatchException?


public class Test
{
    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner input = new Scanner("text.txt");
        int x = input.nextInt();        
    }
}

text.txt being:

8 
8
6  
7

This code throws a InputMismatch Exception. Why?


Solution

  • That is because "text.txt" is not a number. Try:

    Scanner input = new Scanner(new File("text.txt"));