【问题标题】:Output error in Java. Hope for assistance.Java 中的输出错误。希望得到帮助。
【发布时间】:2013-10-15 02:24:46
【问题描述】:

我在BlueJ 中使用Java,我对它还很陌生。我的程序快结束了,但我似乎遇到了障碍。我没有收到任何语法错误,但是在运行程序时它给了我一个错误。

该程序的目的是读取一个 .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.

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

【问题讨论】:

  • 我认为 next int 在单行上查找单个 int。你的文件比那个复杂一点。您需要提取每一行,然后将其分成两部分 - 名称和编号。

标签: java output bluej


【解决方案1】:

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

double sales;

...

sales = infile.nextDouble();

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 2014-11-07
    • 2011-04-17
    • 2013-10-07
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多