Read Data From Text File And Sum Numbers

2019-01-29 04:12发布

I want to read in data from a text file which is full of integers and have the program print those integers out to the screen while summing them. This shouldn't be hard, but I can't figure it out!!!

Here is the extremely simplified text file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

And here is my code that is supposed to work:

import java.util.*;
import java.io.File;
import java.io.IOException;

public class ReadFile
{
    public static void main(String[] args)
    throws IOException
    {
        Scanner textfile = new Scanner(new File("Some_Numbers.txt"));

        filereader(textfile);
    }   


    static void filereader(Scanner textfile)
    {
        int i = 0;
        int sum = 0;

        while(i <= 19)
        {
            System.out.println(textfile.nextInt());
            sum = sum + textfile.nextInt();
            i++;
        }
    }



}

Finally, here is the output I get:

1
3
5
7
9
11
13
15
17
19
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at ReadFile.filereader(ReadFile.java:23)
    at ReadFile.main(ReadFile.java:12)

8条回答
仙女界的扛把子
2楼-- · 2019-01-29 04:21

In addition to lockstock's answer, you might want to consider adding textfile.hasNext() OR textfile.hasNextInt() for your while loop.

static void filereader(Scanner textfile) {
int sum = 0;          
while(textfile.hasNextInt()) {
    int nextInt = textfile.nextInt();
    System.out.println(nextInt);
    sum += nextInt;
}

}

查看更多
Ridiculous、
3楼-- · 2019-01-29 04:21

1. Your i is never Incremented.

2. You are not adding up numbers.

3. addstuff(diskScanner) is a static memeber, so call it with class name with dot operator.

Eg:

Add.addstuff(diskScanner);

   static void addstuff(Scanner aScanner)
{
    int i = 0;
    long l = 0;


    while(i < 25)
    {
        l = i + l;
        i++;
    }

    System.out.println(l);



}
查看更多
爷、活的狠高调
4楼-- · 2019-01-29 04:22

You are calling textfile.nextInt() twice in the loop. Try:

static void filereader(Scanner textfile)     
{         
    int i = 0;         
    int sum = 0;          
    while(i <= 19)         
    {       
        int nextInt = textfile.nextInt();          

        System.out.println(nextInt);             
        sum = sum + nextInt;
        i++;         
    }     
}
查看更多
Root(大扎)
5楼-- · 2019-01-29 04:27

Because you never increment i, it never changes from 0, so it is always less than 25. That means it tries to look again after it has done 25 elements, but there is no 26th, so there's an exception.

Instead of a while loop, I would recommend a for loop.

To sum them, instead of printing them out, just add to an accumulator.

int accumulator = 0;
for(int i = 0; i < 25; i++)
    accumulator += aScanner.nextInt();

System.out.println(accumulator);

should print out the sum.

查看更多
姐就是有狂的资本
6楼-- · 2019-01-29 04:29

You will have to do following thing

    while(i < 25)
    {
        System.out.println(aScanner.nextInt());
        i++ ;
    }
查看更多
三岁会撩人
7楼-- · 2019-01-29 04:33

You never increment i, so the while loop continues beyond the end of the file. As for summing, do something like this.

static void addstuff(Scanner aScanner) {
        int sum = 0;

        while (aScanner.hasNextInt()) {
            sum += aScanner.nextInt();
        }

        System.out.println(sum);
}

This will run until hasNextInt() returns false, which occurs when the next item the scanner sees isn't an integer. This could be non-numeric input or the end of the file. This means you're no longer limited to 25 integers - it'll read as many as it can before stopping.

查看更多
登录 后发表回答