0

poll.txtという名前のファイルからデータ セットを取得し、関連データを使用しようとしています。

poll.txtの内容:

CT 56 31 7 Oct U. of Connecticut

NE 37 56 5 Sep Rasmussen

AZ 41 49 10 Oct Northern Arizona U.

ソースコード、ElectoralVotes.java:

import java.io.*;
import java.util.*;

public class ElectionResults {

    public static void main(String[] args) throws FileNotFoundException {
        int dVotes = 0;
        int sVotes = 0;

        Scanner scanner = new Scanner(new File("poll.txt"));
        String[] nonDigits = new String[29];
        int[] Digits = new int[10];
        int i = 0;
        int j = 1;
        while (scanner.hasNextLine()) {
            while (scanner.hasNext()) {
                nonDigits[i++] = scanner.next();
            }
            i = 0;
            while (j <= 3) {
                Digits[i] = Integer.parseInt(nonDigits[j]);
                i++;
                j++;
            }

            if (Digits[0] > Digits[1]) {
                sVotes += Digits[2];
            } else {
                dVotes += Digits[2];
            }
            scanner.nextLine();
        }
    }
}

ただし、プログラムを実行すると、例外が発生する前に使用されている行は1つだけです。

Exception in thread "main" java.util.NoSuchElementException: No line found error.

「scanner.nextLine();」を動き回ってみました 役に立たない声明。nextLine を要求しなくてもプログラムは正常に動作しますが、明らかにそれが必要であり、何が問題なのかわかりません。

4

4 に答える 4

1

これを試してください..

ポーリング ファイルの形式が変更されず、修正されている場合は、次のコードを試してください。

String area = null;
String personName = null;
while (scanner.hasNextLine())
{
    // e.g to extract "CT" from "CT 56 31 7 Oct U. of Connecticut"
    area = scanner.next(); 

    // e.g to extract "56" from "CT 56 31 7 Oct U. of Connecticut"
    dVotes = scanner.nextInt(); 

    // e.g to extract "31" from "CT 56 31 7 Oct U. of Connecticut"
    sVotes = scanner.nextInt();

    // e.g to skip "7" from "CT 56 31 7 Oct U. of Connecticut"
    scanner.nextInt();

    // e.g to skip "Oct" from "CT 56 31 7 Oct U. of Connecticut"
    scanner.next();

    // e.g to extract "U. of Connecticut" from "CT 56 31 7 Oct U. of Connecticut"
    personName = scanner.nextLine();
    System.out.println(area + " " + dVotes + " " + sVotes + " " + personName);
}

ファイルが現在の poll.txt の形式に従っている場合、上記のコードは正常に機能します。そこに計算を追加する必要があるかもしれません.poll.txtから値を抽出する方法を示しているだけです。

于 2012-04-24T09:37:24.313 に答える
1

scanner.nextLine()ループの最後で呼び出しているため、次の行を読み取ろうとします。あなたはただデータを捨てているだけです!

データが不足しているため、例外がスローされていると思われます。私はあなたのループを疑っています:

while(scanner.hasNext()){
   nonDigits[i++] = scanner.next();
}

完全にデータを消費しています。(Scanner確かに、 の動作は、ドキュメントから私にはそれほど明確ではありませんでした。) 実際には、次のように、一度に 1ずつ読む必要があると思います。

while (scanner.hasNextLine()) {
    String line = scanner.nextline();
    // Now use line
}

...そして行を個別に解析します。

于 2012-04-24T08:21:40.377 に答える
0

私はついにそれを働かせました。もう 1 つの問題は、ループごとに j が再初期化されず、Digits 配列に残っているデータを再利用していたことです。最終的に動作したコードは次のとおりです。

import java.io.*;
import java.util.*;

public class ElectionResults
{
  public static void main (String[] args) 
  throws  FileNotFoundException {
     int dVotes = 0;
     int sVotes = 0;

     Scanner scanner = new Scanner(new File("poll.txt"));
     String [] nonDigits = new String [29];
     int [] Digits = new int [10];
     int i = 0;

     while(scanner.hasNextLine()){
        String line = scanner.nextLine();

        Scanner linescan = new Scanner(line);
        i=0;
        while(linescan.hasNext()){
           String temp = linescan.next();

           nonDigits[i]=temp;
           i++;
        }

        i=0;
        int j = 1;
        while(j<=3)
        {
           Digits[i] = Integer.parseInt(nonDigits[j]);
           i++;
           j++;
        }
        if (Digits[0]>Digits[1])
           sVotes += Digits[2];
        else
           dVotes += Digits[2];  

     }
     System.out.println("Smith Votes:\t" + sVotes);
     System.out.println("Dow Votes:\t" + dVotes);
     if (sVotes > dVotes)
        System.out.println("Smith is currently in the lead!");
     if (dVotes > sVotes)
        System.out.println("Dow is currently in the lead!");
        if(dVotes == sVotes)
        System.out.println("It's a tie!");
  }
}
于 2012-04-24T18:25:27.903 に答える
0

データは内側のループで完全に消費されます。ジョンはすでにそれを指摘しています。この例はあなたを助けるかもしれません:-)

public class ElectionResults {

public static void main(String[] args) throws FileNotFoundException {

    Scanner scanner = new Scanner(new File("c:\\poll.txt"));
    while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         Scanner linescanner = new Scanner(line);
         //this inner loop will scan through the line.
         while(linescanner.hasNext())
         {
             System.out.print(linescanner.next() + " ");
         }
         System.out.println();
         linescanner.close();
    }
    scanner.close();
}

}
于 2012-04-24T09:18:52.237 に答える