假设我有一个文本文件:
2 4 6 7 -999
9 9 9 9 -999
当我运行程序时,我应该在每一行打印出除“-999”之外的所有内容。我应该得到的是:
2 4 6 7
9 9 9 9
这是我尝试过的:
public class Prac {
public static void main(String[] args) throws FileNotFoundException {
Scanner reader = new Scanner(new File("test.txt"));
while(reader.hasNextLine() && reader.nextInt() != -999) {
int nextInt = reader.nextInt();
System.out.print(nextInt + " ");
}
}
}
我试过使用 while/for 循环,但似乎没有让它工作,而且数字不在不同的行上。我不明白为什么当我运行代码时条件不起作用并且打印时每一行都没有分开。我一直在努力寻找解决方案,并决定在这里提问。这可能是一个简单的问题,但我有一段时间没有编码了,所以请告诉我。提前致谢。