Java中的输出错误。希望得到帮助。

时间:2013-10-08 08:45:03

标签: java output bluej

我在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实际上是程序的源文件,所以这不是问题。

这应该是在运行程序即我的输出时创建的.TXT文件REPORT.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.

我似乎无法弄清楚如何克服这个障碍,所以我希望有人可以在这里帮助我。如果有人发现任何潜在的错误,而不是我要求的,请随时指出它们。这将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:2)

您的数据文件包含double类型的数据,而不是int。将变量类型的销售额更改为double,并使用scanner.nextDouble()而不是nextInt()。

double sales;

...

sales = infile.nextDouble();