Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将整数打印到新行,直到某一点_Java - Fatal编程技术网

Java 将整数打印到新行,直到某一点

Java 将整数打印到新行,直到某一点,java,Java,假设我有一个包含以下内容的文本文件: 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

假设我有一个包含以下内容的文本文件:

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

试试这个

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("");
            }
        }
    }

}
while
中的
reader.nextInt()
将使用下一个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();
            }
        }
    }
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
扫描仪阅读器=新扫描仪(新文件(“test.txt”);
列表值=新的ArrayList();
while(reader.hasNextLine()){
int-nextInt=reader.nextInt();
如果(nextInt!=-999){
系统输出打印(nextInt+“”);
添加(nextInt);
}否则{
整数和=0;
for(int值:值){
总和+=数值;
}
System.out.println((float)sum/values.size());
value.clear();
}
}
}

问题是您没有在while循环中保存来自
reader.nextInt()
的值。 您可以尝试以下方法:

while (reader.hasNextLine()) {
    int nextInt = reader.nextInt();
    System.out.print( nextInt != -999 ? nextInt + " " : "\n");
}

我参加聚会迟到了。。。但只要你接受了别人的回复,我想我会分享我开始写的回复。。。但在你收到另外两个(很棒!)回复之前,回复速度太慢了

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()”的值。因此,您跳过了其他所有值

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

    一种方法是执行显式的“close()”

    另一个替代方案(如上图所示)是Java8中引入的


  • @我通常每隔几个月来这里一次,对不起,我没有具体说明。我已经更新了我的问题。谢谢。一旦我看到你的代码;我开始研究一些替代方案。已经有两个人抢先了我;)反应很好。这是一个很好的解决方案,也是对OP原始代码中出现问题的一个很好的解释!谢谢,你知道如何平均每一行吗。例如,在运行第一行之后,它应该是5,然后第二行应该是9。我似乎不能这样做@y、 路易斯