Reputation: 97
I am working on a program which takes the pay rate and hours of employees with given ID numbers, stores them in arrays, and uses a method to determine the total pay of the employee, which will also be stored in an array. My program can accept the pay rate and hours of the employees, but when it outputs the employee ID and corresponding total pay, it returns an odd group of characters such as [D@1909752. How can I get the program to properly output the total pay of the given employee? I think the problem arises somewhere between the method and passing the method to the final for loop, and the fact that my method multiplies an int by a double, but I don't know what to do from here. Thank you for your help. My code is below.
import java.util.Scanner;
class Payroll
{
public static void main(String[] args)
{
int[] employeeId = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
int[] hours = new int[7];
double[] payRate = new double[7];
for(int counter = 0; counter< 7; counter++)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the hours worked by employee #" + employeeId[counter]);
hours[counter] = keyboard.nextInt();
if (hours[counter] < 0)
{
System.out.println("Error: hours cannot be negative. The program will now close.");
System.exit(0);
}
else
{
continue;
}
}
for(int counter1 = 0; counter1< 7; counter1++)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the payrate of employee #" + employeeId[counter1]);
payRate[counter1] = keyboard.nextDouble();
if (payRate[counter1] < 6.0)
{
System.out.println("Error: payrate must be above 6.0. The program will now close.");
System.exit(0);
}
else
{
continue;
}
}
for (int counter2 = 0; counter2 < 7; counter2++)
{
System.out.println("The gross pay for employee #" + employeeId[counter2] + " is " + wages(employeeId, hours, payRate));
}
}
public static double[ ] wages(int[] employeeId, int[] hours, double[] payRate)
{
double[] wages = new double[7];
for(int counter2 = 0; counter2< 7; counter2++)
{
wages[counter2] = hours[counter2] * payRate[counter2];
}
return wages;
}
}
Upvotes: 1
Views: 140
Reputation: 86744
The string [D@1909752
is the default output of toString()
applied to an array of double
. In the statement
System.out.println("The gross pay for employee #" + employeeId[counter2] + " is " + wages(employeeId, hours, payRate));
the method wages
returns an array of double
and you told the program to print the entire array, not one specific element. If you wanted to print all seven elements you would need to print each one individually using a loop or lambda.
Upvotes: 1