我是初学者。使用扫描仪从 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("#|\r\n");
关于java - 使用java扫描仪读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713853/