我刚刚开始学习并用java“玩”了一下。我是计算机工程专业的学生(去年我们学过C)。 但是,今天我遇到了“从文件扫描”的问题。 我在Eclipse中工作,之后我创建了一个文件文本“in.txt”,其中包含:
20 31,12 Luca
我坚持编译器给出的同样错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at it.devapp.corsojava.esempi.FileScanner.main(FileScanner.java:20)
重点是什么?我找不到错误。 这是代码:
public class FileScanner {
public static void main(String[] args)
throws FileNotFoundException { // needed for file operations
int num1;
double num2;
String name;
double sum;
// Scanning input operations
Scanner in = new Scanner(new File("in.txt"));
num1 = in.nextInt();
num2 = in.nextDouble();
name = in.next();
// Display
sum = num1 + num2;
System.out.println("The integer read is: " + num1);
System.out.println("The floating point number read is: " + num2);
System.out.println("The string read is: " + name);
System.out.println("Hi " + name +", the sum of " + num1 + " and " + num2 + " is: " + sum );
in.close();
}
}
感谢大家! :)