0

I'm receiving the following error:

printfile.java:6: error: cannot find symbol

throws FileNotFoundException {
                   ^
  symbol:   class FileNotFoundException
  location: class printfile

for the following code:

import java.io.File;

import java.util.Scanner;

    public class printfile {

        public static void main(String[]args) 

            throws FileNotFoundException {
            Scanner keyboard = new Scanner(System.in);
            System.out.println (" What file are you looking for? ");
            String searchedfile = keyboard.next();
            File file = new File(searchedfile);
            if (file.exists()) {
                System.out.println(" Okay, the file exists... ");
                System.out.print(" Do you want to print the contents of " + file + "?");
                String response = keyboard.next();
                if (response.startsWith("y")) {
                    Scanner filescan = new Scanner(file);
                        while (filescan.hasNext()) {
                        System.out.print(filescan.next());
                        }
                }   
                else {
                    System.out.print(" Okay, Have a good day.");
                }
        }
    }
}

How can this error be resolved?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ashwetzer
  • 11
  • 1
  • 1
  • 1

1 Answers1

3

To use a class that is not in the "scope" of your program, (i.e. FileNotFoundException), you have to:

Call it's fully qualified name:

// Note you have to do it for every reference.
public void methodA throws java.io.FileNotFoundException{...} 
public void methodB throws java.io.FileNotFoundException{...} 

OR

Import the class from it's package:

// After import you no longer have to fully qualify.
import java.io.FileNotFoundException;

...

public void methodA throws FileNotFoundException{...} 
public void methodB throws FileNotFoundException{...} 

Suggest also taking a look in this question, explains pretty much everything you might want to know about Java's acess control modifiers.

Community
  • 1
  • 1
Genos
  • 413
  • 4
  • 12