【发布时间】:2018-03-23 00:07:34
【问题描述】:
我正在编写一个程序,该程序从输入文件(名字和姓氏、小时费率和工作时间)中获取信息,计算净工资并将其写入不同的文件。文件工资.txt 有以下内容:
First Name, Last Name, Hourly pay, Hours Worked
Bruce, Wayne, 7.25, 40
John, Jones, 8.50, 45
Clark, Kent, 7.25, 42
输出应该是:
Last Name, First Name, Total Pay
Wayne, Bruce, 290
Jones, John, 361.25
Kent, Clark, 297.25
加班费是每小时费率的一半。费率是双倍的,工作时间是整数。在我的测试中,输出格式行被写入文件,并在程序收集计算工资的信息时引发异常。
static void wages(){
//input file
String file_name = "wages.txt";
File input_file = new File(file_name);
Scanner in_file = null;
//This is the format line that will be wrote to the output file
String out_format_line = "Last Name, First Name, Total Amount";
//output file
String file2_name = "out.txt";
File output_file = new File(file2_name);
PrintWriter out_file = null;
try{
out_file = new PrintWriter(output_file);
}
catch(FileNotFoundException ex){
System.out.println("Error: The output file does not exist");
ex.printStackTrace();
System.exit(0);
}
try{
in_file = new Scanner(input_file);
in_file.useDelimiter("[,\n]\\\\s");
}
catch(FileNotFoundException ex){
System.out.println("Error: The input file does not exist");
ex.printStackTrace();
System.exit(0);
}
try{
//This line is useless and is removed with this variable
String in_format_line = in_file.nextLine();
//writes format line for output file
out_file.print(out_format_line);
while(in_file.hasNextLine()){
//gathers relavent materials for calulating pay
String first_name = in_file.next();
String last_name = in_file.next();
double rate = in_file.nextDouble();
double over_rate = rate/2;
int hours = in_file.nextInt();
//calculates net pay with overtime
if(hours > 40){
int overtime = hours - 40;
double norm_pay = 40*rate;
double over_pay = overtime * over_rate;
double net_pay = norm_pay + over_pay;
out_file.print(last_name);
out_file.print(", ");
out_file.print(first_name);
out_file.print(", ");
out_file.println(net_pay);
}
//calculates net pay without overtime
else{
double net_pay = hours*rate;
out_file.print(last_name);
out_file.print(", ");
out_file.print(first_name);
out_file.print(", ");
out_file.println(net_pay);
}
}
}
catch(InputMismatchException ex){
System.out.println("Error: File format is incorrect");
ex.printStackTrace();
}
finally{
in_file.close();
out_file.close();
}
}
}
当 ex.printStackTrace() 运行时,控制台显示:
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 guided.exercise.pkg4.GuidedExercise4.wages(GuidedExercise4.java:120)
at guided.exercise.pkg4.GuidedExercise4.main(GuidedExercise4.java:18)
【问题讨论】:
-
请在您的问题涉及异常时包含堆栈跟踪。不要简单地从
catch块内退出程序。相反,请执行ex.printStackTrace()以将有关错误的信息打印到控制台。 -
@VinceEmigh 我以前从来没有用过,所以如果我用错了,请告诉我
-
您错过了
catch,我认为这可能是实际有问题的代码。然后你必须运行它,所以堆栈跟踪出现在控制台中。复制并粘贴到此处。 -
必须密切注意给你的指示,略过帮助是一个坏习惯。 “然后你必须运行它,所以堆栈跟踪出现在控制台中。复制并粘贴到这里。”它会告诉你发生异常的确切行号,所以它会如果您还可以在代码中指出该行所在的位置,请提供帮助。
-
您似乎将分隔符设置为包含
,,但不包含空格。当扫描器标记Wayne, Bruce, 290时,标记290后面会有一个空格。在分隔符中添加空格,这样它就不会影响标记。
标签: java java.util.scanner java-io