我有这段代码:
ArrayList<Employee> ArrEmployee = new ArrayList<Employee>(); // array for employee objects
try {
Scanner txtIn = new Scanner(new File("/Users/PatrickBui/Documents/workspace/COMP 249 - Assignment 3/src/payroll.txt"));
for (int i = 0; i < ArrEmployee.size(); ++i){
while (txtIn.hasNext()) { // looping through the payroll.txt file and creating Employee objects from its data
try{
long EmployeeNumber = txtIn.nextLong();
String EmployeeName = txtIn.next();
String LastName = txtIn.next();
double HoursWorked = txtIn.nextDouble();
double HourlyWage = txtIn.nextDouble();
if (ArrEmployee.get(i).getHourlyWage() > 10.35){
throw new InputMismatchException(); // throws exception if the hourly wage is less than 10.35$
}
ArrEmployee.add(new Employee(EmployeeNumber,EmployeeName,LastName,HoursWorked,HourlyWage));
}
catch (InputMismatchException n) { // catching long,strings and doubles in the payroll.txt that aren't valid
PrintWriter txtOut = new PrintWriter("/Users/PatrickBui/Documents/workspace/COMP 249 - Assignment 3/src/payrollError.txt");
txtOut.println(Employee.EmployeeNumber + " " + Employee.EmployeeName + " " + Employee.LastName + " " + Employee.HoursWorked + " " + Employee.HourlyWage);
txtOut.close();
}
}
}
我试图找出一种可以看到的方法,因为我的catch (InputMismatchException n)
将变量EmployeeNumber,[...],HourlyWage写入名为payrollError.txt的文件
我已经尝试了很多次但是payrollError.txt文件没有任何问题......我的代码肯定有问题,但我似乎无法弄清楚
答案 0 :(得分:0)
我会评论,但声誉不让我......
以下代码适用于我:
public class Test {
public static void main(String[] args) throws InterruptedException, IOException {
File f = new File("test.txt");
f.createNewFile();
PrintWriter txt = new PrintWriter("test.txt");
txt.println("testing, testing, 1 2 3");
txt.close();
}
}