我的简要介绍是写一些关于员工的数据(将年龄作为整数验证,将薪水验证为双倍。)一旦经过验证,必须逐行将信息写入文件。必须有3个员工记录。
我的当前代码,如下所示,接受12行数据并将它们逐行放入文件中。我不确定的是如何验证输入。我需要名字和标题作为任何东西,然后我需要年龄作为整数和薪水双倍。
public static void main(String[] args) {
Scanner employeeData = new Scanner (System.in);
System.out.println("Please enter the employee name, title, age and salary. Press enter after each piece of information: ");
try {
File file = new File("employee.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < 12; i++) {
bw.write(employeeData.next() + System.getProperty("line.separator"));
}
bw.close();
}catch (IOException e1) {
System.err.println("The file doesn't exist.");
}
catch (InputMismatchException e2) {
System.err.println("Incorrect data type. Name and job title can be string, age whole number and Salary must be double (with decimals).");
}
finally{
System.out.println("Done.");
}
employeeData.close();
}
}