Search code examples
javacompiler-errorsprintln

I keep getting "error:Cannot find symbol" in println


I know this is a duplicate, but none of the others seem to help with my issue, and I can't find anything on Google that helps.

Here is my code:

import java.util.Scanner;

class File_Scanner{    
    public static void read(){
        try {
            File credentials_file = new File("credentials.txt");
            Scanner file_reader = new Scanner(credentials_file);
            String[] users = new String[6];
            int index_counter = 0;

            while (file_reader.hasNextLine()) {
                users[index_counter] = file_reader.nextLine();
                index_counter++;
            }

            file_reader.close();
        }
        catch (Exception e) {
          System.out.println(e.getClass());
        }
        System.out.println(users[0]);
    }
}

The error I get is:

File_Scanner.java:19: error: cannot find symbol
        System.out.println(users[0]);
                           ^
  symbol:   variable users
  location: class File_Scanner
1 error

Thanks for all of your help! You guys are the best!


Solution

  • users is defined inside of your try block, so it's not a valid variable outside of the try block.