1

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;
   }

}
3
  • Maybe just do this: wages(employeeId, hours, payRate)[counter2] Commented Apr 22, 2016 at 20:43
  • No, counter2 is not visible at that point. It is local to wages. Commented Apr 22, 2016 at 20:46
  • It worked because I used counter2 in the main method too to make it less confusing for me Commented Apr 22, 2016 at 20:49

1 Answer 1

1

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.

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

2 Comments

Maybe just do this: wages(employeeId, hours, payRate)[counter2]
Thank you Mr. Garrison for the information, and jgitter, that worked perfectly. Thank you all!

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.