morskiq123
morskiq123

Reputation: 39

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;
}

}

Upvotes: 0

Views: 99

Answers (1)

Pedro Lima
Pedro Lima

Reputation: 1606

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.

Upvotes: 3

Related Questions