I am getting an error in my java code and I am wondering what I need to do to fi
ID: 3569638 • Letter: I
Question
I am getting an error in my java code and I am wondering what I need to do to fix it.
Here is my code:
import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Employees_3_10 {
final static double FEDTAX = .18;
final static double STTAX = .045;
static Toolkit tools = new Toolkit();
public static void main (String[]args) throws Exception {
Scanner input = new Scanner(System.in);
// Define file names
final String INPUT_FILE = "Employees_3_10_Input.txt";
final String OUTPUT_FILE = "Employees_3_10_Output.txt";
// Access the input/output files
Scanner InputFile = new Scanner(new FileReader (INPUT_FILE));
Scanner InputFileCount = new Scanner(new FileReader (INPUT_FILE));
PrintWriter OutputFile = new PrintWriter(OUTPUT_FILE);
int lines = countLines(InputFileCount);
InputFileCount.close();
// Define Variables
double [][] data = new double [30][7];
String [] names = new String [30];
names = new String[lines];
data = new double[lines][2];
input(names, data, InputFile);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
sortNamesAlpha(names, data);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
sortGrossPay(names, data);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
OutputFile.close();
}
//************************************************** ****************************************
public static int input(double [][] data, String [] names, Scanner inputFile)
{
int i = 0;
int m = 0;
String line;
StringTokenizer st;
while (inputFile.hasNext() && i < names.length)
{
line = inputFile.nextLine();
st = new StringTokenizer(line);
m = st.countTokens();
names [i] = st.nextToken();
for (int j = 1; j < m-2; j++)
names [i] = names [i] + " " + st.nextToken();
data [i][5] = Double.parseDouble(st.nextToken());
data [i][6] = Double.parseDouble(st.nextToken());
i++;}
return i;
}
//*******************************************************************************************
public static void getName(String[] names, int i, StringTokenizer st){
int m = st.countTokens();
names[i] = st.nextToken();
for (int j = 1; j < m - 2; j++){
names[i] = names[i] + " " + st.nextToken();
}
}
//*******************************************************************************************
public static int countLines(Scanner InputFile){
String tmp;
int i = 0;
while (InputFile.hasNext()){
tmp = InputFile.nextLine();
i++;
}// end while
return i;
}
//*******************************************************************************************
public static double roundNumber(double valueToRound, int placesToRound) {
double roundMe; // The number to round, starts as valueToRound
double powerOfTen; // To calculate 10^placesToRound
roundMe = valueToRound; // Ex: round 1.256, 2 places
powerOfTen = Math.pow(10,placesToRound); // 10 ^ 2 = 100
roundMe = roundMe * powerOfTen; // 1.256 * 100 = 125.6
roundMe = Math.round(roundMe); // 125.6 rounded = 126
return roundMe / powerOfTen; // 126 / 100 = 1.26
} // End roundNumber
//************************************************** ****************************************
public static void printHeading(PrintWriter OutputFile){
int widthR = 17;
int widthL = 12;
OutputFile.println("|---------------------------------------------------" +
"------------------------------------------|");
OutputFile.print(rightPadName("Name", widthR));
OutputFile.print(leftPad("Net Pay", widthL));
OutputFile.print(leftPad("Gross Pay", widthL));
OutputFile.print(leftPad("Federal Tax", widthL));
OutputFile.print(leftPad("State Tax", widthL));
OutputFile.print(leftPad("Hours", 8));
OutputFile.print(leftPad("Pay Rate|", 13));
OutputFile.print(" ");
OutputFile.println("|---------------------------------------------------" +
"------------------------------------------|");
}// end print Heading
//************************************************** ****************************************
public static void printEmployeeLines(String[] names, double[][] data, PrintWriter OutputFile){
//name, net pay, gross pay, fed tax, state tax, dues, hours, pay rate
for (int i=0; i< names.length; i++){
int widthR = 17;
int widthL = 12;
String name = names[i];
OutputFile.print(rightPadName(name, widthR));
OutputFile.print(leftPadCurrency(calcNetPay(data, i), widthL));
OutputFile.print(leftPadCurrency(calcGrossPay(data, i), widthL));
OutputFile.print(leftPadCurrency(calcFedTax(data, i), widthL));
OutputFile.print(leftPadCurrency(calcStTax(data, i), widthL));
OutputFile.print(leftPad(data[i][0], 8));
OutputFile.print(leftPadCurrency(data[i][1], widthL));
OutputFile.print("| ");
}// end for
}
//************************************************** ****************************************
public static double calcNetPay(double[][] data, int i){
return (calcGrossPay(data, i) - calcFedTax(data, i) -
calcStTax(data, i));
}// end clac net pay
//*******************************************************************************************
public static double calcGrossPay(double[][] data, int i){
if (data[i][0] <= 40.0){
return (data[i][0] * data[i][1]);
}
else if (data[i][0] < 50){
return (((data[i][0] - 40) * 1.5 * data[i][1]) + data[i][1]*40);
}
else if (data[i][0] >= 50){
return (((data[i][0] - 50) * 2 * data[i][1]) + data[i][1] * 40 +
15 * data[i][1]);
}
else{
return 0;
}
}// end calc GrossPay
//************************************************** ****************************************
public static double calcFedTax(double[][] data, int i){
return (calcGrossPay(data, i) * FEDTAX);
}// End calcFedTax
public static double calcStTax(double[][] data, int i){
return (calcGrossPay(data, i) * STTAX);
}// end calcStTax
public static void sortNamesAlpha(String[] names, double[][] data){
for(int i = 0; i < names.length; i++){
for (int j = 0; j < data.length; j++){
if(names[i].compareTo(names[j]) > 0){
swapValues(data, i, j);
swapNames(names, i, j);
}//end if
}// end for j
}// end for i
}// end sortNamesAlpha
//*******************************************************************************************
public static void sortGrossPay(String[] names, double[][] data){
for(int i = 0; i < data.length; i++) {
for(int j = i; j < data.length; j++) {
if(calcGrossPay(data, i) > calcGrossPay(data, j)){
swapValues(data, i, j);
swapNames(names, i, j);
}// end if
}// end for
}// end for
}// end sortGrossPay
//******************************************************************************************
public static void swapValues(double[][] data, int i, int j) {
double temp = data[i][0];
data[i][0] = data[j][0];
data[j][0] = temp;
double temp2 = data[i][1];
data[i][1] = data[j][1];
data[j][1] = temp2;
}// end swapValues
//******************************************************************************************
public static void swapNames(String[] names, int i, int j) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}// end swap names
//******************************************************************************************
public static void printSummary(String[] names, double[][] data, PrintWriter OutputFile){
int widthR = 17;
int widthL = 12;
OutputFile.println("|-----------------------------------------------------"+
"----------------------------------------|");
OutputFile.print(rightPadName("Total", widthR));
OutputFile.print(leftPadCurrency(calcTotalNetPay(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalGrossPay(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalFedTax(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalStTax(data), widthL));
OutputFile.print(leftPad(calcTotalHours(data), 8));
OutputFile.print(" | ");
OutputFile.println("|-----------------------------------------------------"+
"----------------------------------------|");
}// end print summary
//********************************************************************************************
public static double calcTotalNetPay(double[][] data){
double temp = 0;
for(int i = 0; i < data.length; i++){
temp += calcNetPay(data, i);
}
return temp;
}// end calc total net pay
//********************************************************************************************
public static double calcTotalGrossPay(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcGrossPay(data, i);
}
return temp;
}// end calc total gross pay
//*********************************************************************************************
public static double calcTotalHours(double[][] data){
double temp = 0;
for(int i = 0; i< data.length; i++){
temp += data[i][0];
}
return temp;
}// end calc total hours
//*********************************************************************************************
public static double calcTotalFedTax(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcFedTax(data, i);
}
return temp;
}// end calc total fed tax
//**********************************************************************************************
public static double calcTotalStTax(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcStTax(data, i);
}
return temp;
}// end calc total st tax
//**********************************************************************************************
public static String leftPadCurrency(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.00");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
if(m == 5 && i == 3 || m == 4 && i == 4 || m == 8 && i == 0){
s = "$" + s;
}
else if (m == 6 && i == 2)
s = "$" + s;
else{
s = " " + s;
}
}
return s;
} // end leftPadCurrency
//**********************************************************************************************
public static String leftPadDues(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.00");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
if(m == 4 && i == 2 || m == 6 && i == 0){
s = "$" + s;
}
else{
s = " " + s;
}
}
return s;
}
// left pad takes numbers
//***********************************************************************************************
public static String leftPad(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.0");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
s = " " + s;
}
return s;
} // end leftPad
//***********************************************************************************************
// overloaded leftPad takes string
public static String leftPad(String s, int width){
int m = s.length();
for(int i=0; i < width - m; i++){
s = " " + s;
}
return s;
} // end leftPad overloaded
//***********************************************************************************************
public static String rightPadName(String s, int width){
int m = s.length();
for(int i=0; i < width - m; i++){
s = s + " ";
}
s = "|" + s;
return s;
}// end rightPadName
}
The error is here:
Employees_3_10.java:39: error: incompatible types: String[] cannot be converted to double[][]
input(names, data, InputFile);
Explanation / Answer
import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Employees_3_10 {
final static double FEDTAX = .18;
final static double STTAX = .045;
/**static Toolkit tools = new Toolkit();**/
public static void main (String[]args) throws Exception {
Scanner input = new Scanner(System.in);
// Define file names
final String INPUT_FILE = "Employees_3_10_Input.txt";
final String OUTPUT_FILE = "Employees_3_10_Output.txt";
// Access the input/output files
Scanner InputFile = new Scanner(new FileReader (INPUT_FILE));
Scanner InputFileCount = new Scanner(new FileReader (INPUT_FILE));
PrintWriter OutputFile = new PrintWriter(OUTPUT_FILE);
int lines = countLines(InputFileCount);
InputFileCount.close();
// Define Variables
double [][] data = new double [30][7];
String [] names = new String [30];
names = new String[lines];
data = new double[lines][2];
input(data, names, InputFile);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
sortNamesAlpha(names, data);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
sortGrossPay(names, data);
printHeading(OutputFile);
printEmployeeLines(names, data, OutputFile);
printSummary(names, data, OutputFile);
OutputFile.close();
}
//************************************************** ****************************************
public static int input(double [][] data, String [] names, Scanner inputFile)
{
int i = 0;
int m = 0;
String line;
StringTokenizer st;
while (inputFile.hasNext() && i < names.length)
{
line = inputFile.nextLine();
st = new StringTokenizer(line);
m = st.countTokens();
names [i] = st.nextToken();
for (int j = 1; j < m-2; j++)
names [i] = names [i] + " " + st.nextToken();
data [i][5] = Double.parseDouble(st.nextToken());
data [i][6] = Double.parseDouble(st.nextToken());
i++;}
return i;
}
//*******************************************************************************************
public static void getName(String[] names, int i, StringTokenizer st){
int m = st.countTokens();
names[i] = st.nextToken();
for (int j = 1; j < m - 2; j++){
names[i] = names[i] + " " + st.nextToken();
}
}
//*******************************************************************************************
public static int countLines(Scanner InputFile){
String tmp;
int i = 0;
while (InputFile.hasNext()){
tmp = InputFile.nextLine();
i++;
}// end while
return i;
}
//*******************************************************************************************
public static double roundNumber(double valueToRound, int placesToRound) {
double roundMe; // The number to round, starts as valueToRound
double powerOfTen; // To calculate 10^placesToRound
roundMe = valueToRound; // Ex: round 1.256, 2 places
powerOfTen = Math.pow(10,placesToRound); // 10 ^ 2 = 100
roundMe = roundMe * powerOfTen; // 1.256 * 100 = 125.6
roundMe = Math.round(roundMe); // 125.6 rounded = 126
return roundMe / powerOfTen; // 126 / 100 = 1.26
} // End roundNumber
//************************************************** ****************************************
public static void printHeading(PrintWriter OutputFile){
int widthR = 17;
int widthL = 12;
OutputFile.println("|---------------------------------------------------" +
"------------------------------------------|");
OutputFile.print(rightPadName("Name", widthR));
OutputFile.print(leftPad("Net Pay", widthL));
OutputFile.print(leftPad("Gross Pay", widthL));
OutputFile.print(leftPad("Federal Tax", widthL));
OutputFile.print(leftPad("State Tax", widthL));
OutputFile.print(leftPad("Hours", 8));
OutputFile.print(leftPad("Pay Rate|", 13));
OutputFile.print(" ");
OutputFile.println("|---------------------------------------------------" +
"------------------------------------------|");
}// end print Heading
//************************************************** ****************************************
public static void printEmployeeLines(String[] names, double[][] data, PrintWriter OutputFile){
//name, net pay, gross pay, fed tax, state tax, dues, hours, pay rate
for (int i=0; i< names.length; i++){
int widthR = 17;
int widthL = 12;
String name = names[i];
OutputFile.print(rightPadName(name, widthR));
OutputFile.print(leftPadCurrency(calcNetPay(data, i), widthL));
OutputFile.print(leftPadCurrency(calcGrossPay(data, i), widthL));
OutputFile.print(leftPadCurrency(calcFedTax(data, i), widthL));
OutputFile.print(leftPadCurrency(calcStTax(data, i), widthL));
OutputFile.print(leftPad(data[i][0], 8));
OutputFile.print(leftPadCurrency(data[i][1], widthL));
OutputFile.print("| ");
}// end for
}
//************************************************** ****************************************
public static double calcNetPay(double[][] data, int i){
return (calcGrossPay(data, i) - calcFedTax(data, i) -
calcStTax(data, i));
}// end clac net pay
//*******************************************************************************************
public static double calcGrossPay(double[][] data, int i){
if (data[i][0] <= 40.0){
return (data[i][0] * data[i][1]);
}
else if (data[i][0] < 50){
return (((data[i][0] - 40) * 1.5 * data[i][1]) + data[i][1]*40);
}
else if (data[i][0] >= 50){
return (((data[i][0] - 50) * 2 * data[i][1]) + data[i][1] * 40 +
15 * data[i][1]);
}
else{
return 0;
}
}// end calc GrossPay
//************************************************** ****************************************
public static double calcFedTax(double[][] data, int i){
return (calcGrossPay(data, i) * FEDTAX);
}// End calcFedTax
public static double calcStTax(double[][] data, int i){
return (calcGrossPay(data, i) * STTAX);
}// end calcStTax
public static void sortNamesAlpha(String[] names, double[][] data){
for(int i = 0; i < names.length; i++){
for (int j = 0; j < data.length; j++){
if(names[i].compareTo(names[j]) > 0){
swapValues(data, i, j);
swapNames(names, i, j);
}//end if
}// end for j
}// end for i
}// end sortNamesAlpha
//*******************************************************************************************
public static void sortGrossPay(String[] names, double[][] data){
for(int i = 0; i < data.length; i++) {
for(int j = i; j < data.length; j++) {
if(calcGrossPay(data, i) > calcGrossPay(data, j)){
swapValues(data, i, j);
swapNames(names, i, j);
}// end if
}// end for
}// end for
}// end sortGrossPay
//******************************************************************************************
public static void swapValues(double[][] data, int i, int j) {
double temp = data[i][0];
data[i][0] = data[j][0];
data[j][0] = temp;
double temp2 = data[i][1];
data[i][1] = data[j][1];
data[j][1] = temp2;
}// end swapValues
//******************************************************************************************
public static void swapNames(String[] names, int i, int j) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}// end swap names
//******************************************************************************************
public static void printSummary(String[] names, double[][] data, PrintWriter OutputFile){
int widthR = 17;
int widthL = 12;
OutputFile.println("|-----------------------------------------------------"+
"----------------------------------------|");
OutputFile.print(rightPadName("Total", widthR));
OutputFile.print(leftPadCurrency(calcTotalNetPay(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalGrossPay(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalFedTax(data), widthL));
OutputFile.print(leftPadCurrency(calcTotalStTax(data), widthL));
OutputFile.print(leftPad(calcTotalHours(data), 8));
OutputFile.print(" | ");
OutputFile.println("|-----------------------------------------------------"+
"----------------------------------------|");
}// end print summary
//********************************************************************************************
public static double calcTotalNetPay(double[][] data){
double temp = 0;
for(int i = 0; i < data.length; i++){
temp += calcNetPay(data, i);
}
return temp;
}// end calc total net pay
//********************************************************************************************
public static double calcTotalGrossPay(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcGrossPay(data, i);
}
return temp;
}// end calc total gross pay
//*********************************************************************************************
public static double calcTotalHours(double[][] data){
double temp = 0;
for(int i = 0; i< data.length; i++){
temp += data[i][0];
}
return temp;
}// end calc total hours
//*********************************************************************************************
public static double calcTotalFedTax(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcFedTax(data, i);
}
return temp;
}// end calc total fed tax
//**********************************************************************************************
public static double calcTotalStTax(double[][] data){
double temp = 0;
for( int i = 0; i < data.length; i++){
temp += calcStTax(data, i);
}
return temp;
}// end calc total st tax
//**********************************************************************************************
public static String leftPadCurrency(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.00");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
if(m == 5 && i == 3 || m == 4 && i == 4 || m == 8 && i == 0){
s = "$" + s;
}
else if (m == 6 && i == 2)
s = "$" + s;
else{
s = " " + s;
}
}
return s;
} // end leftPadCurrency
//**********************************************************************************************
public static String leftPadDues(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.00");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
if(m == 4 && i == 2 || m == 6 && i == 0){
s = "$" + s;
}
else{
s = " " + s;
}
}
return s;
}
// left pad takes numbers
//***********************************************************************************************
public static String leftPad(double num, int width){
DecimalFormat fmt = new DecimalFormat("#,##0.0");
String s = fmt.format(num);
int m = s.length();
for(int i=0; i < width - m; i++){
s = " " + s;
}
return s;
} // end leftPad
//***********************************************************************************************
// overloaded leftPad takes string
public static String leftPad(String s, int width){
int m = s.length();
for(int i=0; i < width - m; i++){
s = " " + s;
}
return s;
} // end leftPad overloaded
//***********************************************************************************************
public static String rightPadName(String s, int width){
int m = s.length();
for(int i=0; i < width - m; i++){
s = s + " ";
}
s = "|" + s;
return s;
}// end rightPadName
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.