null
null

Reputation: 35

What is the correct way to do this? Initialization/Symbol not found errors

I've been practicing with Scanner and exceptions and they're fairly new so maybe I'm missing something here:

This first way the compiler says it cannot find filename or f.

do {
            try {
                System.out.print("Enter the file name:  ");
                String filename = k.nextLine();
                Scanner f = new Scanner(new File(filename));
                done = true;
            }
            catch(FileNotFoundException ex1){
                System.out.println("The file "+filename+" does not exist.");
            }
            catch(Exception e){
                System.out.println("Unpredicted exception");
            }
        } while (!done);

I thought I'd put it outside to fix it, but this other way it complains f and filename may have not been initialized

Scanner k = new Scanner(System.in), f;
        String filename;
        boolean done = false;
// Request file from the user
        do {
            try {
                System.out.print("Enter the file name:  ");
                filename = k.nextLine();
                f = new Scanner(new File(filename));
                done = true;
            }
            catch(FileNotFoundException ex1){
                System.out.println("The file "+filename+" does not exist.");
            }
            catch(Exception e){
                System.out.println("Unpredicted exception");
            }
        } while (!done);

Upvotes: 0

Views: 380

Answers (3)

Bohemian
Bohemian

Reputation: 425063

A variable's scope is the block in which it's declared. Your first version declared filename in the try block, but you were trying to use it in the catch block (which is a different block despite being part of the one try-catch syntactical structure.

Your second attempt fixes this by declaring the variable with a scope that includes all places it is used, however in java local variables have no default initialization value; you must provide a value if there are code paths that could lead to accessing the variable before it has had a value assigned to it.

The fix is to provide any value (including null) to the variable before the try-catch., either at declaration time, or in the lines immediately following the declaration.

Upvotes: 1

Paramvir Singh Karwal
Paramvir Singh Karwal

Reputation: 616

You must always declare and initialize variables in such conditions before the 'try' block starts. Otherwise you will have to face with these errors: 'Cannot find the symbol' , 'Variable not initialized' .

In your code you have declared the String variable outside the 'try' block which is fine, but the problem is that you have not initialized it. Hope it solves your problem.

Upvotes: 1

Matt O
Matt O

Reputation: 1346

Initialise the value of filename to null. That way, if the catch block is entered, it will still have a value to use when you're printing the error message.

String filename = null;

Upvotes: 1

Related Questions