简体   繁体   中英

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!

users是在try块内定义的,因此它不是try块外的有效变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM