我正在使用Java
它BlueJ
,我对它还很陌生。我的程序快结束了,但我似乎遇到了障碍。我没有收到任何语法错误,但是在运行程序时它给了我一个错误。
该程序的目的是读取 .TXT 文件,并根据其中的信息,创建一个包含新信息的新 .TXT 文件。
这是唯一的课程Program3
:
import java.io.*;
import java.util.*;
public class Program3 {
public static void main(String[] args) {
int sales;
String name;
double baseSalary, commission, overCommission, underCommission, salary;
Scanner infile;
PrintWriter outfile;
try {infile = new Scanner(new FileReader("PROG3IN.TXT"));}
catch (IOException err) {
infile = null;
System.out.println("Cannot open PROG3IN.TXT.");
System.exit(1);
}
try {outfile = new PrintWriter(new FileWriter("REPORT.TXT"));}
catch (IOException err) {
outfile = null;
System.out.println("Cannot create REPORT.TXT.");
System.exit(1);
}
outfile.printf
("Salesman Sales Commission Salary%n");
while (infile.hasNext()) {
name = infile.next();
sales = infile.nextInt(); //ERROR
if (sales > 1000.00) {
overCommission = sales - 1000.00;
overCommission = overCommission * .08;
underCommission = 1000.00 * .05;
commission = overCommission + underCommission;
} else
commission = sales * .05;
commission = Math.rint(commission * 100.00) / 100.00;
baseSalary = 200.00;
salary = baseSalary + commission;
outfile.printf("%-12s %11.2f %11.2f %11.2f%n",
name, sales, commission, salary);
}
infile.close();
outfile.close();
}
}
在程序运行期间,我收到一条错误消息:
sales = infile.nextInt();
根据 BlueJ,这就是问题所在:
java.until.InputMismatchException;
null (in.java.util.Scanner)
为清楚起见,以下内容PROG3IN.TXT
包含:
COSTA 1000.00
LOMBARDI 852.16
MARTINEZ 1043.57
THOMAS 714.23
YOUNG 1104.95
注意:PROG3IN.TXT
实际上是在程序的源文件中,所以这不是问题。
这是REPORT.TXT
运行程序时创建的 .TXT 文件中应该包含的内容,也就是我的输出:
Salesman Sales Commission Salary
COSTA 1000.00 50.00 250.00
LOMBARDI 852.16 42.61 242.61
MARTINEZ 1043.57 53.49 253.49
THOMAS 714.23 35.71 235.71
YOUNG 1104.95 58.40 258.40
以防万一需要更清楚,这是手头的具体任务:
Each salesman for a small company earns a weekly base salary of
$200.00 plus a commission. The commission is 5 percent of sales
for sales of up to $1000.00. The commission is 8 percent on
sales in excess of $1000.00. Thus for $800.00 in sales a
salesman earns a base salary of $200.00, plus a commission of
$40.00, for a total salary of $240.00. For $1200.00 in sales a
salesman earns a base salary of $200.00, plus a commission of
$50.00 on the first $1000.00 of sales, plus a commission of
$16.00 on the remaining $200.00 of sales, for a total salary of
$266.00.
Write a program which computes the commission and salary for
each salesman in the company. Round each commission to the
nearest cent before adding it to the base salary. The data file
PROG3IN.TXT contains one line of data for each salesman
containing the last name and the sales for the week. Process
this data file and print a report in the following format.
我似乎无法弄清楚如何克服这个障碍,所以我希望有人能在这里帮助我。如果有人看到我要求的以外的任何潜在错误,请随时指出。这将不胜感激。先感谢您。