1

I am trying to load a file called SPY.txt into an array, but I can't even get this little snippet to work.

I don't understand. If f.exists is true, how can the Scanner throw a file not found exception?

import java.io.*;
import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args) {
        File f = new File (new File("SPY.txt").getAbsolutePath());
        System.out.println(f.exists());
        Scanner s = new Scanner(f);
    }
}

Output: True

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown at scannertest.ScannerTest.main(ScannerTest.java:13)

Line 13 is

Scanner s = new Scanner(f);

Maroun
  • 94,125
  • 30
  • 188
  • 241
user2991144
  • 21
  • 1
  • 3

3 Answers3

5

FileNotFoundException is a checked exception that's thrown by that particular Scanner constructor. Either declare it with a throws clause, or put a try-catch block in there.

This has nothing to do with whether the file exists or not, but everything to do with exception handling in Java.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
5

The clue is in the error message:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown at scannertest.ScannerTest.main(ScannerTest.java:13)

What it means is that Scanner constructor throws an exception, so you need to place it in try/catch block, like so:

import java.io.*;
import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args) {
        //try block starts here
        try {  
            File f = new File (new File("SPY.txt").getAbsolutePath());
            System.out.println(f.exists());
            Scanner s = new Scanner(f);
        } 
        //catch the exception
        catch(FileNotFoundException e) {
            e.printStackTrace();   
        }

    }
}

Check the docs here and here

Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • Wow. Thank you. My original program had a try/finally. I guess I've got to read up on the try. Thank you very much for your help. – user2991144 Nov 14 '13 at 09:43
1

You are not getting an exception that the file is not found, you are getting an error about Uncompilable source code because you didn't handle an exception.

You have "Unhandled exception type FileNotFoundException" in:

new Scanner(f)

Solutions:

  1. Surround with try-catch.
  2. Declare main to throw FileNotFoundException.

//1
try {
    File f = new File (new File("SPY.txt").getAbsolutePath());
    System.out.println(f.exists());
    Scanner s = new Scanner(f);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

OR

//2
public static void main(String[] args) throws FileNotFoundException {
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • He isn't getting an exception at all. He is getting a compile error. – user207421 Nov 14 '13 at 09:41
  • The code in your revised answer is still useless, as `s` isn't live outside the `try` block, and the word that should be italicized is not 'exception' but 'handle'. – user207421 Nov 14 '13 at 10:06
  • @EJP I was only demonstrating what should be done, not providing a full code as you see. – Maroun Nov 14 '13 at 10:07
  • I see, but does the OP? If you provide code it should be useful. People are going to just cut-and-paste it without the least thought. – user207421 Nov 14 '13 at 10:26
  • @EJP Since I explained, writing actual code is only for demonstration, OP needs to changed small things before running the program. The most important thing for me is the concept. However, I changed it :) – Maroun Nov 14 '13 at 10:27