2

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)
4

8 に答える 8

4

をインクリメントiしないため、while ループはファイルの末尾を超えて継続します。加算については、次のようにします。

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

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

        System.out.println(sum);
}

これは、 hasNextInt()が false を返すまで実行されます。これは、スキャナが次に検出するアイテムが整数ではない場合に発生します。これは、数値以外の入力またはファイルの終わりである可能性があります。これは、25 個の整数に制限されなくなったことを意味します。停止する前に、できるだけ多くの整数を読み取ります。

于 2012-08-08T16:59:20.977 に答える
4

をインクリメントiしないため、0 から変化することはないため、常に 25 未満です。つまり、25 要素を実行した後に再度検索しようとしますが、26 番目がないため、例外があります。

ループの代わりに、whileループをお勧めしforます。

それらを合計するには、出力する代わりに、アキュムレータに追加するだけです。

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

System.out.println(accumulator);

合計を出力する必要があります。

于 2012-08-08T16:59:38.960 に答える
2

lockstock の回答に加えて、 while ループに textfile.hasNext() OR textfile.hasNextInt() を追加することを検討してください。

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

}

于 2012-08-09T01:25:27.063 に答える
1

ファイルに整数型に収まらない非常に大きな数値があるため、エラー (例外) が発生しています。Longを使用する必要があります。

したがって、aScanner.nextLong()の代わりに: を使用しaScanner.nextInt()ます。

編集:sum変数の型も からintに変更する必要がありますlong

あなたのコードは正常に動作します。

于 2012-08-08T18:48:39.550 に答える
1

次のことを行う必要があります

    while(i < 25)
    {
        System.out.println(aScanner.nextInt());
        i++ ;
    }
于 2012-08-08T17:02:02.747 に答える
1

iループ内でインクリメントしないため、例外がスローされます。入力ファイル内の整数の数が不明な場合は、Scanner.hasNextInt()メソッドを使用して次の整数の存在を確認できます (または単にhasNext()EOF を確認するために使用します)。

以下のコード スニペットのように数値を合計できます。

    ...
    int sum = 0;
    while(i++ < 25)
    {
        sum += aScanner.nextInt();
    }

また

    ...
    int sum = 0;
    while(aScanner.hasNextInt())
    {
        sum += aScanner.nextInt();
    }
于 2012-08-08T17:04:41.500 に答える
0

1.あなたi決して増分されません。

2.あなたは数を合計していません。

3. addstuff(diskScanner)は、なので、ドット演算子をstatic memeber使用してクラス名で呼び出します。

例えば:

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);



}
于 2012-08-08T17:07:33.563 に答える