Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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_File_Bufferedreader_Filereader - Fatal编程技术网

如何从Java文件中读取一行中的每个整数?

如何从Java文件中读取一行中的每个整数?,java,file,bufferedreader,filereader,Java,File,Bufferedreader,Filereader,我有一个包含n行整数的文件。我想在每行中添加每个int并打印它们(因此我应该在末尾打印3个int,每行一个int) 我尝试了这个方法,但它将读取并添加第一个循环中的所有整数 scan = new Scanner(new BufferedReader(new FileReader("input.txt"))); int n = scan.nextInt(); for (int i = 0; i < n; i++) { while (scan.hasNextLine()) {

我有一个包含n行整数的文件。我想在每行中添加每个int并打印它们(因此我应该在末尾打印3个int,每行一个int)

我尝试了这个方法,但它将读取并添加第一个循环中的所有整数

scan = new Scanner(new BufferedReader(new FileReader("input.txt")));
int n = scan.nextInt();
for (int i = 0; i < n; i++) {
    while (scan.hasNextLine()) {
         sum += scan.nextInt();
    }
    System.out.println(sum);
    sum = 0;
}
scan=newscanner(newbufferedreader(newfilereader)(“input.txt”));
int n=scan.nextInt();
对于(int i=0;i
此行为是由于错误使用循环和扫描造成的。 其中一个正确的解决方案还包含java 8 lambda,假设文件中整数的分隔符为空格(“”):


这种行为是由于错误使用循环和扫描造成的。 其中一个正确的解决方案还包含java 8 lambda,假设文件中整数的分隔符为空格(“”):


我想我也可以这么做

read = new Scanner(new BufferedReader(new FileReader("input.txt")));
int n = read.nextInt(), j;
int sum = 0;
for (int i = 0; i < n; i++) 
{
    String[] str;
    int t = read.nextInt();
    // First str will be ""(it reads the end of each line) 
    str = read.nextLine().split(" ");
    // Then it can read what we want
    str = read.nextLine().split(" ");
    for (j = 0; j < str.length; j++) 
    {
        sum += Integer.parseInt(str[j]);
    }
    System.out.println(sum);
    sum = 0;                
} 
read=newscanner(newbufferedreader(newfilereader)(“input.txt”));
int n=read.nextInt(),j;
整数和=0;
对于(int i=0;i
我想我也可以这么做

read = new Scanner(new BufferedReader(new FileReader("input.txt")));
int n = read.nextInt(), j;
int sum = 0;
for (int i = 0; i < n; i++) 
{
    String[] str;
    int t = read.nextInt();
    // First str will be ""(it reads the end of each line) 
    str = read.nextLine().split(" ");
    // Then it can read what we want
    str = read.nextLine().split(" ");
    for (j = 0; j < str.length; j++) 
    {
        sum += Integer.parseInt(str[j]);
    }
    System.out.println(sum);
    sum = 0;                
} 
read=newscanner(newbufferedreader(newfilereader)(“input.txt”));
int n=read.nextInt(),j;
整数和=0;
对于(int i=0;i
所以每一行都应该是当前整数和所有先前整数的总和?是的,同一行中的所有先前整数为了更快地获得更好的帮助,请添加或。在
字符串中硬编码数据
以替换
input.txt
。因此,每一行都应该是当前整数和所有先前整数的总和?是的,同一行中的所有先前整数为了更快地获得更好的帮助,请添加或。将数据硬编码为
字符串
,以替换
input.txt
。但是我应该使用BufferedReader或其他Buffery之类的东西。这个条件以前从未提到过。但是如果你看一下Files.lines()的实现,它使用了BufferedReader,所以如果你坚持的话,基本上你可以从那里复制。但是我应该使用BufferedReader或其他Buffery之类的东西。这个条件以前从未提到过。但是,如果您查看Files.lines()的实现,它使用BufferedReader,因此如果您坚持,基本上可以从那里进行复制。