我有一个 Person 类,其中创建了 getter 和 setter,以及变量名称(String)、age(int)、height(double)、width(double)。我想从文件中读取信息并将适当的人员数据分配给这些变量。
- setName 接受一个字符串值
- setAge 采用 int 值
- setHeight 和 setWidth 取双精度值
一切看起来都很简单,但是程序在线程“main”java.util.InputMismatchException中抛出了一个错误异常
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class ObjectOriented {
public static void main(String args[]) throws IOException{
Scanner inputFile=new Scanner(new File("src\\statistics.txt"));
Person dima=new Person();
dima.setName(inputFile.nextLine());
dima.setAge(inputFile.nextInt());
dima.setHeight(inputFile.nextDouble());
dima.setWidth(inputFile.nextDouble());
dima.userInformation();
}
}
userInformation() 显示有关用户的信息。
面纱本身看起来像这样:
Dima
20
172.5
61.9
替换文本文件中的点,或者如果不可能,使用解析。另请注意,在 nextInt 方法之后,您需要调用 nextLine。
原则上,这种方法对我来说似乎更好,因为在 nextDouble 里面仍然是一样的