How to use Scanner to read only one line at a time with integers?

问题: If I have a .txt file with a list of numbers. It should return the sum for all the numbers in each line as well as the sum of every number in the file. Then print all of th...

问题:

If I have a .txt file with a list of numbers. It should return the sum for all the numbers in each line as well as the sum of every number in the file. Then print all of this in the console. Lets say the txt file is:

50  3   21  10  9   9   54  47  24  74
22  63  63  28  36  47  60  3   45  83
20  37  11  41  47  89  9   98  40  94
48  77  93  68  8   19  81  67  80  64
41  73  24  29  99  6   41  23  23  44
43  41  29  11  43  94  62  27  81  71
83  14  97  67  21  68  77  25  21  24
31  8   54  14  49  96  33  18  14  80
54  55  53  38  62  53  62  10  42  29
17  89  92  87  15  42  50  85  68  43

This is my code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Summer {
    public static void main(String args[]) throws IOException {

        File text = new File("src/nums.txt");

        if (!text.exists()) {
          text.createNewFile();
        }

        int sum = 0;
        Scanner input = new Scanner(text);
        while (input.hasNextInt()) {
            sum = sum + input.nextInt();

        }

        System.out.printf("Sum of all numbers: %d", sum);


        int lineSum = 0;
        int lineNum = 1;

        while (input.hasNext()) {
            if (input.hasNextInt()) {
                lineSum = lineSum + input.nextInt();
            } else {
                input.next();
                lineNum++;
            }
        }

        System.out.printf("%nSum of line %d: %d", lineNum, lineSum);
    }
}

Which outputs:

Sum of all numbers: 4687
Sum of line 1: 0

回答1:

Your second loop will never work since after the first one you are at the EOF (End-Of-File) and the scanner object will not start over from the beginning.

The best thing here is to use 2 Scanner objects, one to read a line from the file and one to read the values from that line. With this solution you can count total per line and file total at once.

int total = 0;
Scanner input = new Scanner(text);
while (input.hasNextLine()) {
    Scanner lineScanner = new Scanner(input.nextLine());
    int lineSum = 0;
    while (lineScanner.hasNextInt()) {
        lineSum += lineScanner.nextInt();
    }
    System.out.println(Sum of line is: " + lineSum);
    total += lineSum;
}
System.out.println("File sum is: " + total);

My printing is a little different than yours but that's easy to fix.


回答2:

Problem:

Your problem here is that you are using the same Scanner instance to read the file twice, which is causing the problem because it already reaches the end of the file in the first while call, so when you recall input.hasNext() it will be false thus you won't enter the second while.

Solution:

You need to re-initialize the input Scanner just before the second while call:

int lineSum = 0;
int lineNum = 1;

//Re initialize the scanner instance here
input = new Scanner(text);
while (input.hasNext()) {
    //Do the calculations
}

Note:

You need also to pay attention to the input.nextInt() and input.next() calls in your calculations to get the desired behavior.

  • 发表于 2019-03-22 01:55
  • 阅读 ( 239 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除