我是初学者。使用扫描器从Java文本文件中读取。仅读取前3个令牌的代码不起作用:
try{
Scanner scFile = new Scanner (new File ("readhere.txt")).useDelimiter("#");
String first = scFile.next();
String second = scFile.next();
int third = scFile.nextInt(); // error here. Why cant I store the integer?
}
catch(FileNotFoundException e){
System.out.println("Error");
}
我正试图读取前3个令牌:
Andrew#Smith#21
John#Morris#55
在读取21时出现问题。java.util.InputMismatchException
扫描程序将回车字符作为下一个可读令牌的一部分,该令牌会产生一个无效整数。你可以写
Scanner scanner = new Scanner(new File("readhere.txt")).useDelimiter("#|rn");