2

假设我有一个文本文件:

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 循环,但似乎没有让它工作,而且数字不在不同的行上。我不明白为什么当我运行代码时条件不起作用并且打印时每一行都没有分开。我一直在努力寻找解决方案,并决定在这里提问。这可能是一个简单的问题,但我有一段时间没有编码了,所以请告诉我。提前致谢。

4

4 回答 4

3

reader.nextInt()in将while消耗下一个 int,因此您将始终跳过一个整数。所以我建议:

    public static void main(String[] args) throws FileNotFoundException {
        Scanner reader = new Scanner(new File("test.txt"));
        while (reader.hasNextLine()) {
            int nextInt = reader.nextInt();
            if (nextInt != -999)
                System.out.print(nextInt + " ");
            else
                System.out.println();
        }
    }

更新:如果您想按照评论中的要求计算每行的平均值,您可以存储每个值以进行计算(请参阅此处的其他方式)。下面的代码将执行此操作并在行尾打印平均值:

    public static void main(String[] args) throws FileNotFoundException {
        Scanner reader = new Scanner(new File("test.txt"));
        List<Integer> values = new ArrayList<>();
        while (reader.hasNextLine()) {
            int nextInt = reader.nextInt();
            if (nextInt != -999) {
                System.out.print(nextInt + " ");
                values.add(nextInt);
            } else {
                int sum = 0;
                for (int value : values) {
                    sum += value;
                }
                System.out.println((float) sum / values.size());
                values.clear();
            }
        }
    }
于 2018-12-09T01:36:23.620 回答
2

试试这个。

public static void main(String[] args) throws FileNotFoundException {

    Scanner reader = new Scanner(new File("./input.txt"));
    while (reader.hasNextInt()) {
        int nextInt = reader.nextInt();
        if (nextInt != -999) {
            System.out.print(nextInt + " ");
        } else {
            if (reader.hasNextLine()) {
                System.out.println("");
            }
        }
    }

}
于 2018-12-09T01:36:14.187 回答
2

问题是您没有从reader.nextInt()while 循环中保存值。你可以试试这个:

while (reader.hasNextLine()) {
    int nextInt = reader.nextInt();
    System.out.print( nextInt != -999 ? nextInt + " " : "\n");
}
于 2018-12-09T02:24:20.847 回答
2

我参加聚会迟到了……但只要在你接受后有人回复,我想我会分享我开始写的回复……但是在你得到另外两个之前回复太慢了(太棒了! ) 回应。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Prac {

    static final int EOL = -999;

    public static void main(String[] args) throws FileNotFoundException {
        File f = new File("test.txt");
        try (Scanner reader = new Scanner(f)) {
            int nextInt;
            while(reader.hasNext()) {
                if ((nextInt = reader.nextInt()) == EOL)
                    System.out.println();
                else
                    System.out.print(nextInt + " ");
            }
        }
    }
}

注意: 1. 主要问题是您没有在 while 循环中捕获“scanner.nextInt()”的值。因此,您跳过了所有其他值。

  1. 还有一个资源泄漏 - 你没有关闭扫描仪。对于像这样的小程序来说这并不重要(退出程序会很好地关闭文件;))。

    一种方法是做一个明确的“close()”。

    如上所示,另一种替代方法是Java 8 中引入的try-with-resources 语句。

于 2018-12-09T03:11:24.443 回答