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

但是,当我运行程序时,在给出异常之前只使用了其中一行:

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

我试过移动“scanner.nextLine();” 声明无济于事。如果我不要求 nextLine,该程序可以正常工作,但我显然需要它,而且我似乎无法弄清楚出了什么问题。

4

4 回答 4

1

您在scanner.nextLine()循环结束时调用,它将尝试读取下一行。然后你只是扔掉数据!

我怀疑这会引发异常,因为您的数据已用完。我怀疑你的循环:

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

完全消耗数据。(Scanner诚​​然,我从文档中对我的行为从来没有非常清楚。)我怀疑你实际上应该一次阅读一行,其中:

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

...并分别解析该行。

于 2012-04-24T08:21:40.377 回答
1

请试试这个..

如果 poll 文件的格式不会改变并且是固定的,那么请尝试以下代码..

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 回答
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 回答
0

我终于让它工作了。另一个问题是 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 回答