Reputation: 623
I am using Java
in BlueJ
and I am fairly new to it. I am nearing the end of my program but I seem to have run into a snag. I don't get any syntax errors but when running the program it gives me an error.
The purpose of this program is to read a .TXT file and based on the information in that, create a new .TXT file with new information.
Here is the only class, 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();
}
}
During the running of the program, I am getting an error on the line:
sales = infile.nextInt();
According to BlueJ, this is the problem:
java.until.InputMismatchException;
null (in.java.util.Scanner)
For clarity, this is what PROG3IN.TXT
contains:
COSTA 1000.00
LOMBARDI 852.16
MARTINEZ 1043.57
THOMAS 714.23
YOUNG 1104.95
Note: PROG3IN.TXT
is in fact in the source file of the program, so that is not the issue.
This is what is supposed to be in the .TXT file REPORT.TXT
that is created when running the program aka my output:
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
Just in case more clarity is required, this is the specific task at hand:
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.
I can't seem to figure out how to overcome this obstacle, so I hope someone can assist me here. If anyone sees any potential errors other than what I'm asking for feel free to point them out. It would be greatly appreciated. Thank you in advance.
Upvotes: 1
Views: 145
Reputation: 420
Your data file contains data of type double, not int. Change the variable type of sales to double, and use scanner.nextDouble() instead of nextInt().
double sales;
...
sales = infile.nextDouble();
Upvotes: 2