0

我正在尝试读取如下所示的文本文件:

A,32,0,0,0,0

现在我有一个从该文件中读取的静态方法。我得到了这个 NoSuchElement 异常,而之前我有一个 Mismatch 异常。

请问我在这段代码中缺少什么?我很抱歉含糊不清。

public static ArrayList<RaceCar> readCar(String s, Track raceTrack)throws IOException,FileNotFoundException
 {
     Scanner sc = new Scanner(new File("CarData.txt"));
     sc.useDelimiter(",");
     String exists;
     ArrayList<RaceCar> racers = new  ArrayList<RaceCar>();

    while ((exists = sc.nextLine()) != null) 
    {
         String dName = sc.next();
         int dNum = sc.nextInt();
         int dWins = sc.nextInt();
         int dRunUp = sc.nextInt();
         int dRaces = sc.nextInt();

         racers.add(new RaceCar(dName,dNum,raceTrack,dWins,dRunUp,dRaces));
    }   

    return racers;
 }
4

2 回答 2

1

代替

sc.nextLine()

while (sc.hasNext()) {
  //code
} 

通过调用 nextLine,文件中该行的所有数据都在exists.

看看JavaDoc

于 2013-04-24T08:47:17.580 回答
0

你需要exists-String做什么?

尝试:

while (sc.hasNextLine()) {
   // ...
}
于 2013-04-24T08:58:03.250 回答