简体   繁体   中英

How to populate Array of Strings from text file

I'm trying to get the information from Stock.txt and to transfer it into an array of strings, each index being a new line in the file. I get a warning:

Duplicate local variable. What is the problem, is it out of scope?

public static List<String> getStock(List<String> stockText){
    Scanner scanner = null;
    try {
        File input = new File("Stock.txt");
        scanner = new Scanner(input);
        String[] info = null;
        while (scanner.hasNextLine()) {
            info = scanner.nextLine().split(",");
        }
        List<String> stockText = Arrays.asList(info);
    }   catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    finally {
        scanner.close();
    }
    return stockText;
}

}

As it is, stockText is an argument and later you create a variable with the same name. That's not allowed. If your intention was to use the same variable, remove List<String> from List<String> stockText = Arrays.asList(info);

Otherwise, give the variable another name.

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