【发布时间】:2018-01-14 21:01:36
【问题描述】:
我想要一个方法来返回在 while 循环中出现的值。我的代码代表读取一个 txt 文件,我在其中逐行读取,我的目标是每次找到一行时都返回,但一遍又一遍地向我显示相同的数字。
public String getInputsTxtFromConsole() {
String line = "";
//read inputs file
try {
Scanner scanner = new Scanner(inputFile);
//read the file line by line
int lineNum = 0;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
lineNum++;
//Return statement does not work here
}
} catch (FileNotFoundException e) {
}
return "";
}
【问题讨论】:
-
你能分享一个示例输入文件和你试图得到的结果吗?这会让问题更清楚。
-
输入是字符串,如:96464444; 96464644;94646464 等
-
我只想在 while 循环中单独返回每个数字
-
return 语句 exits 函数,它不会像 python 中的 yield 那样一直给你所有的值,创建一个你想要的行数组然后返回数组。
-
但我想单独返回,而不是同时返回
标签: java while-loop