public static void main(String[] args) throws FileNotFoundException {
File file = new File("data.txt"); // select file
try{
Scanner sc = new Scanner(file); // set scanner to file
try{
while (sc.hasNextLine()){
Employee employee = new Employee(); //create employee to hold data
assignData(sc); //read data into employee
employee.getGross(); //calculate gross pay
addEmployee(employee);//assign employee to array
}//end while
}//end try2
finally {
sc.close(); // close file, saving resource usage
}//end finally
}//end try1
catch(IOException e) {
e.printStackTrace();
}
public static void assignData(Scanner input){
//accept scanner
//read necessary input for employee
EmpID = input.next();****
LastName = input.next();****
FirstName = input.next();****
(LINE 36)Hours = input.nextDouble();
Rate = input.nextDouble();
}
数据文件内容
42345 Bruch Max 40 21.50
23456 Elgar Eddie 43 20.0
34567 Bach John 30 30
12345 Wagner Rick 41 30
88888 Mozart Wolfie 36 40
65432 Chopin Fred 45 23.25
72345 Strauss Butch 50 25
编译,我得到错误
"Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at programassignment1c.Employee.assignData(Employee.java:36) (
at programassignment1c.ProgramAssignment1C.main(ProgramAssignment1C.java:44)
Java 结果:1
Employee 类中的第 36 行,上面突出显示,它是 nextDouble() 我在做什么错?即使没有小数位,也可以扫描双精度数吗?
手写这个我看到扫描仪开始于:42345 并将其设置为 EmpID,然后扫描仪前进过去的空白到
Bruch 并将其设置为 LastName,然后扫描仪将空格前进到 Max 并将其设置为 FirstName,然后扫描仪将空格前进到 40 并将其设置为 Hours???? 我没有正确跟随扫描仪?