I'm having trouble with a particular part of my program for the end of this (my first) semester. So I must take a text file, which includes names, and some numbers and do some calculations with the numbers for each person. I've tried to create a 2 dimensional array with loops, and convert numbers (which are saved as strings) into doubles. The problem being that I cannot use values for say j = 3 and j = 4 for calculations, as that is the other half of the assignment.
So I need a way to store values of j = 0 and 1 as strings, and values of j = 2, 3, and 4 as doubles --preferably in the same array if possible.
My professor (who doesn't teach much keep in mind) said something about using multiple methods. If you take a look at the following lines:
double empPay = empHoursResult * empRateResult;
System.out.println(arr[3] * arr[4]);
Neither of these work because I either need to initialize the doubles (which make them = 0) and values for arr[j] are stored as strings.
How can I store values of j > 1 as doubles only, and j = 0, 1 as strings? Any advice is appreciated. Thanks.
public static void main(String[] args) {
String[] textLine = new String[10];
int i = 0;
String empNumber;
String empHours;
String empRate;
double empNumberResult;
double empHoursResult;
double empRateResult;
System.out.println("Reading File ......");
String fileName = "datatext.txt";
try {
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
while ((line = bufferReader.readLine()) != null) {
textLine[i] = line;
i++;
}
for (int x = 0; x < i; x++) { //For loop of the rows, each employee.
String empInfo = textLine[x];
String[] arr = empInfo.split(" ");
System.out.println("\nEmployee: " + (x + 1));
for (int j = 0; j < arr.length; j++) {
if (j == 0) {
System.out.println("Last Name: " + arr[j]);
} else if (j == 1) {
System.out.println("First Name: " + arr[j]);
} else if (j == 2) {
empNumber = arr[j];
empNumberResult = Double.parseDouble(empNumber);
System.out.println("Employee Number: " + empNumberResult);
} else if (j == 3) {
empHours = arr[j];
empHoursResult = Double.parseDouble(empHours);
System.out.println("Total Hours Worked: " + empHoursResult);
} else if (j == 4) {
empRate = arr[j];
empRateResult = Double.parseDouble(empRate);
System.out.println("Employee Hourly Rate: " + empRateResult); // Read above line ^^.
}
}
double empPay = empHoursResult * empRateResult;
System.out.println(arr[3] * arr[4]);
/*
* Here is where I want the system to print out calculations from below.
* The calculatePay method must be somewhere in the first 'for loop' because I need it
* to calculate pay for all employess, x.
*/
}
bufferReader.close();
} catch (Exception e) {
System.out.println("Error while reading file line by line:" + e.getMessage());
}
}
public static void calculatePay(double empHoursResult, double empRateResult) {
double empNormalPay;
double empOvertimePay;
double empOvertimeHours;
double empTotalPay;
if (empHoursResult > 40) {
empNormalPay = 40 * empRateResult;
empOvertimeHours = 40 - empHoursResult;
empOvertimePay = empOvertimeHours * 1.5;
empTotalPay = empNormalPay = empOvertimePay;
}
}
empNumber = arr[2];etc.