使用扫描仪读取 java 中的文本文件
Reading text file in java using scanner
我是初学者。使用扫描仪读取 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
扫描器将回车 return 字符作为下一个可读标记的一部分,这会产生一个无效整数。你可以做
Scanner scanner = new Scanner(new File("readhere.txt")).useDelimiter("#|\r\n");
我是初学者。使用扫描仪读取 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
扫描器将回车 return 字符作为下一个可读标记的一部分,这会产生一个无效整数。你可以做
Scanner scanner = new Scanner(new File("readhere.txt")).useDelimiter("#|\r\n");