【发布时间】:2020-04-23 11:27:32
【问题描述】:
我正在尝试从 Stock.txt 中获取信息并将其传输到字符串数组中,每个索引都是文件中的一个新行。我收到警告:
重复的局部变量。是什么问题,是否超出范围?
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;
}
}
【问题讨论】:
-
stockText是您的输入参数的名称和您在try块中创建的局部变量 -
您将
stockText作为参数传递给该方法,然后创建另一个同名的局部变量。您从不使用参数,因此您可以将其从声明中删除。 -
您是否尝试创建扫描仪类的对象,即 Scanner input = new Scanner(input);
-
感谢大家的输入,我现在明白问题所在了! :)