2

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;
    }
}
1
  • 1
    Are you sure you want to store strings and doubles in the same array? Maybe you are supposed to create an object for each employee and store those objects in an array. Btw. your inner for loop is not needed. Just do empNumber = arr[2]; etc. Commented Dec 10, 2016 at 18:33

3 Answers 3

1

Your code seems pretty redundant with all those for loops I've included a bit of pseudo code below from which you should be able to re work your program since I shouldn't solve your assignment.

while(line = readline){
    String[] data = line.split(" ");

    String a = data[0];
    String b = data[1];
    double c = Double.parseDouble(data[2]);
    double d = Double.parseDoubledata([3]);
    double e = Double.parseDoubledata([4]);
}  

Does this clear things up or am I not seeing where you're stuck?

EDIT
Seeing travis post I understand your problem might be in storing both in one array. That's indeed not possible, using two arrays should solve your problems tough.

Sign up to request clarification or add additional context in comments.

Comments

0

Excluding object polymorphism (which doesn't apply here), you can't store two different data types in the same array. By definition, an array contains only one data type. So you either need to create two different arrays (one for your strings and one for your doubles) or create a single "EmployeeInfo" class that contains fields for your strings and doubles--and then create a single array out of that class.

2 Comments

You could store String and Double values in an Object array but i doubt he is supposed to do that.
Since he is saving values 2-4 in other variables I dont think that's actually part of the problem.
0

Assuming using arrays of primitives isn't an explicit requirement for your assignment, you should make an EmployeeWorkRecord object instead that encapsulates each set of items in a record (last name, first name, employee id, hours, rate) and use a collection (like an ArrayList) to store them. You can then add a "calculatePay()" method to your EmployeeWorkRecord.

A primitive array can only store one type of object, so the only way you could do this using only primitive arrays would be to make five parallel arrays, one for each data item. I can't think of any good reasons why you'd want to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.