我正在尝试从名为 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,程序工作正常但我显然需要它,我似乎无法弄清楚什么是错的。
答案 0 :(得分:1)
你在循环结束时调用scanner.nextLine()
,这将尝试阅读下一行。那你就是扔掉数据!
我怀疑是抛出异常,因为你的数据用完了。我怀疑你的循环:
while(scanner.hasNext()){
nonDigits[i++] = scanner.next();
}
完全消费数据。 (诚然,Scanner
的行为对我来说从来都不是非常清楚。)我怀疑你实际上应该一次阅读行,其中包括:
while (scanner.hasNextLine()) {
String line = scanner.nextline();
// Now use line
}
...并单独解析该行。
答案 1 :(得分: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中提取值。
答案 2 :(得分: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();
}
}
答案 3 :(得分: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!");
}
}