Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Trying to get values from arrays rather than hashcode

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Trying to get values from arrays rather than hashcode

    Hello,

    I've been stuck on this homework assignment for some time now, and tried looking for possible solutions. The assignment goes like this...Write a Payroll class that uses the following arrays as fields:
    employeeID - An array of seven integers to hold employee identification numbers.
    The array should be initialized with the following numbers:
    5658845 4520125 7895122 8777541
    8451277 1302850 7580489
    hours - An array of seven integers to hold the number of hours worked by each employee.
    payRate - An array of seven doubles to hold each employee's hourly pay rate.
    wages - An array of seven doubles to hold each employee's gross wages.
    The class should relate the data in each array through the subscripts.
    For example, the number in element 0 of the hours array should be the number of hours worked by the employee
    whose identification number is stored in element 0 of the employeeID array.
    That same employee's pay rate should be stored in element 0 of the payRate array.
    In addition to the appropriate accessor and mutator methods,
    the class should have a method that accepts an employee's identification number
    as an argument and returns the gross pay for that employee.
    Demonstrate the class in a complete program that displays each employee number
    and asks the user to enter that employee's hours and pay rate.
    It should then display each employee's identification number and gross wages.
    Input Validation: Do not accept negative values for hours or numbers less than 6.0 for a pay rate.

    My problem with this program is that everytime I try to print the employee ID's or the wages, I get hashcode or something like it (#[I1a77cdc or something like that). I tried using the toString method, but it lists all of the values, when I'm trying to display one at a time. Here is the code for the class:

    // moduleArray class
    public class moduleArray
    {
       final int NUM_EMPLOYEES = 7;
       int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
       int[] hours = new int[NUM_EMPLOYEES];
       double[] payRate = new double[NUM_EMPLOYEES];
       double[] wages = new double[NUM_EMPLOYEES];
     
       // setHours method
       public void setHours(int[] time)
       {
          hours = time;
       }
     
       // setPayRate method
       public void setPayRate(double[] pay)
       {
          payRate = pay;
       }
     
       //getEmployeeID method
       public int[] getEmployeeID()
       {
          return employeeID;
       }   
     
       // getWages method
       public double[] getWages()
       {
          wages = hours[] * payRate[];
     
          return wages;
       }
     
    }
    This is the demo program to list the ID's. I've been messing with it for some time, and right now I just want it to display values. Any help is appreciated!

    import java.util.Scanner;
     
    public class moduleArrayDemo
    {
       public static void main(String[] args)
       {
          final int NUM_EMPLOYEES = 7;
          int[] ID = new int[NUM_EMPLOYEES];
          int[] time = new int[NUM_EMPLOYEES];
          double[] pay = new double[NUM_EMPLOYEES];
     
          Scanner keyboard = new Scanner(System.in);
     
          moduleArray[] employee = new moduleArray[NUM_EMPLOYEES];
     
          for (int i = 0; i < employee.length; i++)
             employee[i] = new moduleArray();         
     
          for (int i = 0; i < 7; i++)
             System.out.println(employee[i]);      
     
       }
     
    }
    Last edited by ACrappyDJ; March 4th, 2014 at 09:38 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    print the employee ID's
    To see the contents of an array when printing it, use the Arrays class's toString() method to format the array's contents:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    What println() statement are you talking about?
    something like it (#[I1a77cdc or something like that).
    You need to copy the exact text of the error message and paste it here.
    "something like" is missing something important about the error.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ACrappyDJ (March 4th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    Thanks for replying. Sorry about the vagueness. Ultimately, I want the program to display the employeeID # and prompt the user to enter the amount of hours worked, and then pay rate.

    for (int i = 0; i < employee.length; i++)
          {
             System.out.print("Enter hours for Employee #" + employee[i].getEmployeeID() +
                              ": ");
             time[i] = keyboard.nextInt();
             employee[i].setHours(time);
     
             System.out.print("Enter how much Employee #" + employee[i].getEmployeeID() +
                              " makes per hour: ");
             pay[i] = keyboard.nextDouble();
             employee[i].setPayRate(pay);
     
          }
     
          for (int i = 0; i < employee.length; i++)
          {
             System.out.println("Employee #" + employee[i].getEmployeeID() + 
                                " Wages: " + employee[i].getWages());
     
          }

    I tried wrapping the code. Sorry, I'm a super-beginner. Anyways, when I run the program, and just throw in "10" to every input to test it out, I get hash code in all of the answers:

    Employee #[I@6d084b Wages: [D@3901c6
    Employee #[I@3bb2b8 Wages: [D@a37368
    Employee #[I@152544e Wages: [D@edc3a2
    Employee #[I@1cdeff Wages: [D@1c6f579
    Employee #[I17471e0 Wages: [D@11ddcde
    Employee #[I@1e04cbf Wages: [D@18fb1f7
    Employee #[I@cec0c5 Wages: [D@ed0338

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    What do the methods: getEmployeeID() and getWages() return? It looks like they are returning an array instead of a single variable.
    If you want to print the contents of an array see the suggestion in post#2

    Looking at the names of the methods:
    getEmployeeID() looks like it should return a single id
    getWages() is ambiguous. It could return either a single wage or an array of wages. The s on the end makes it look plural. getWage() would be singular and return a single wage
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ACrappyDJ (March 4th, 2014)

  7. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    I think that might be where the problem lies. I want it to return each individual employeeID and wage per subscript. I want it to list employee[0] with wage[0], employee[1] with wage[1], and so on. If I do the toScript method, it lists the entire array, whereas I want to do one value at a time, like the for loop in post #3.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    Those two methods are defined to return an array. Change their definitions to return a single value and pass the index to the array to the method so it can use that index to get the value from the array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ACrappyDJ (March 4th, 2014)

  10. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    So, I changed the definitions to return a single value, but I'm confused about passing the index to the array to the method. This is what I have so far:

    //getEmployeeID method
       public int getEmployeeID(int[] employeeID)
       {
     
          return employeeID;
     
       }   
     
         // getWage method
       public double getWage(double[] wages)
       {
     
          return wages;
     
       }

    Would passing the index to the array involve a for loop?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    The index for an array is an int. To pass an int variable to a method:
    public int someMethod(int arrayIndex) {

    The code that calls that method would have to compute or generate the index. Perhaps the index would be the control variable in a for loop.

    The code you post does nothing. It just returns the exact same array that was passed to it as a parameter.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    Ok, so it displays the employeeID value and wage value for each subscript. Now, it just displays zero for each value. I don't know what it is I'm doing wrong. Is it because I don't have a constructor, or what? I'm completely lost with this...I really appreciate your patience.

     public class moduleArray2
    {
       final int NUM_EMPLOYEES = 7;
       int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
       int[] hours = new int[NUM_EMPLOYEES];
       double[] payRate = new double[NUM_EMPLOYEES];
       double[] wages = new double[NUM_EMPLOYEES];
       int employee = 0;
       double wage = 0;
     
     
       // setHours method
       public void setHours(int[] time)
       {
          time = hours;
     
       }
     
       // setPayRate method
       public void setPayRate(double[] pay)
       {
          pay = payRate;
     
       }
     
        // setWages method
       public void setWage(int[] time, int[] pay)
       {
          for (int index = 0; index < NUM_EMPLOYEES; index++)
             wages[index] = time[index] * pay[index];
     
       }
     
       //getEmployeeID method
       public int getEmployeeID(int index)
       {
     
          return employee;
     
       }   
     
         // getWage method
       public double getWage(int index)
       {
     
     
          return wage;
     
       }
     
    }

    import java.util.Scanner;
     
    public class moduleArrayDemo2
    {
       public static void main(String[] args)
       {
          final int NUM_EMPLOYEES = 7;
          int[] time = new int[NUM_EMPLOYEES];
          double[] pay = new double[NUM_EMPLOYEES];
     
          // Create new Scanner object
          Scanner keyboard = new Scanner(System.in);
     
          // Create employee object
          moduleArray2[] employee = new moduleArray2[NUM_EMPLOYEES];
     
          // A loop that creates objects for each element
          for (int i = 0; i < employee.length; i++)
             employee[i] = new moduleArray2();
     
     
          for (int i = 0; i < employee.length; i++)
          {
             System.out.print("Enter hours for Employee #" + employee[i].getEmployeeID(i) +
                              ": ");
             time[i] = keyboard.nextInt();
             employee[i].setHours(time);
     
             System.out.print("Enter how much Employee #" + employee[i].getEmployeeID(i) +
                              " makes per hour: ");
             pay[i] = keyboard.nextDouble();
             employee[i].setPayRate(pay);
     
          }
     
          for (int i = 0; i < employee.length; i++)
          {
             System.out.println("Employee #" + employee[i].getEmployeeID(i) + 
                                " Wages: " + employee[i].getWage(i));
     
          }    
     
       }
     
    }

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    displays zero for each value.
    Look back through the code to see where the values are coming from.
    What does the getWage() method do with the int arg that is passed to it? If it is not used, it should be removed from the code. The getWage() method returns the value in the wage variable. Where is there a value assigned to the wage variable?
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    ACrappyDJ (March 5th, 2014)

  15. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get values from arrays rather than hashcode

    I went back and assigned the index to the returning variable, and it worked like a charm.

    public double getWage(int index)
    {
      return wage[index];
    }
    Thanks again for your help. I didn't realize that I was passing array arguments into the methods, and trying to pull an integer from it. I'll start small, with one method at a time from now on.

Similar Threads

  1. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  2. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  3. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  4. What are HashCode and equals in Java and thay are implement in java?
    By diyaots in forum Java Theory & Questions
    Replies: 2
    Last Post: September 9th, 2011, 08:38 AM
  5. Program to populate all array values using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 7th, 2009, 05:31 AM