我正在尝试读取一个文本文件并创建一个对象数组。我不断收到以下错误...
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Prog6.main(Prog6.java:33)
它没有读取字段,我已经尝试了我能想到的一切来修复它。这是代码。任何意见,将不胜感激。谢谢!
import java.io.*;
import java.util.*;
public class Prog6
{
public static void main(String[] args)
{
String fname;
String lname;
String team;
String position;
int completions;
int attempts;
int yards;
int receptions;
Scanner inFile = null;
Report rep = new Report();
/*
* Open File
*/
try
{
inFile = new Scanner( new File( "nfl.txt" ) );
}
catch ( FileNotFoundException e )
{
System.err.println( "Error: file not found" );
}
/*
* Read file
*/
while (inFile.hasNext())
{
fname = inFile.next();
lname = inFile.next();
team = inFile.next();
position = inFile.next();
if (position == "QB")
{
completions = inFile.nextInt();
attempts = inFile.nextInt();
yards = inFile.nextInt();
Player qb = new Player ();
rep.addQuarterback(qb);
}
else if (position == "WR")
{
receptions = inFile.nextInt();
yards = inFile.nextInt();
Player wr = new Player ();
rep.addReceiver(wr);
}
// Print report
rep.printReport();
}
}
}