[英]How do I read a certain string on a line from a file, using scanner?
I'm trying to read a text file which looks like this: 我正在尝试读取一个文本文件,如下所示:
A,32,0,0,0,0 A,32,0,0,0,0
And right now I have a static method which reads from that file. 现在,我有一个从该文件读取的静态方法。 I'm getting this NoSuchElement exception, while earlier I had a Mismatch exception.
我收到了这个NoSuchElement异常,而之前我有一个Mismatch异常。
May I ask what I'm missing in this code? 我可以问一下这段代码中我缺少什么吗? I'm sorry for the vagueness.
对不起,我很抱歉。
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;
}
What do you need the exists-String for? 您需要存在字符串做什么?
Try: 尝试:
while (sc.hasNextLine()) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.