我想讀取文本文件並創建一個對象數組。我不斷收到以下錯誤...Java文本文件無法讀取
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();
}
}
}
nfl.txt是怎麼樣的? – JRR 2013-04-10 02:48:40
*「任何建議,將不勝感激..'Prog6.main(Prog6.java:33)'」*看看'Prog6.java'的第33行,並在行尾添加一個註釋,像'//這是它失敗的地方!'。現在您已經確定了該行,請嘗試打印其中引用的任何對象的值。 – 2013-04-10 02:53:29
catch之後的代碼的每一部分都應該放在try中。 – 2013-04-10 02:55:33