nuke_infer
nuke_infer

Reputation: 63

passing 2D arrays into constructor

can you please give me the idea how to pass 2D arrays into the constructor.i have passed the arrays with values but iam getting wrong answer.here with iam sharing my code.

    package payroll;
    import java.util.Scanner; 
    import javax.swing.*;

    class Payroll
    {
     private String empID;   
     private String empName;
     private int numOfHours;
     private double hourlyRate;
     private double totalPay;

     public Payroll(){}

     public Payroll(Object[][]emp, int scale)
     {
      for(int k=0;k<emp.length;k++)
      {
         this.empID = emp[k][l].toString();
         this.empName = emp[k][l].toString();
         this.numOfHours = Integer.parseInt(emp[k][l].toString()); 
         this.hourlyRate = Integer.parseInt(emp[k][l].toString());
       }
  }
 }

     public String getID()
     {
     return empID;
     }
     public String getName()
     {
     return empName;
     }
     public int getNumOfHours()
     {
     return numOfHours;
     }
     public double getHourlyRate()
     {
     return hourlyRate;
     }

     public double gettoTotalPay()
     {
     return numOfHours * hourlyRate;
     }

    }


    class Employer{
             public static void main(String [] args)
      {
            String empId; 
            String empName;
            String numOfHrs;
            String hourlyRate;
            Payroll emp1 = new Payroll();
            Object [][]emp = new Object[1][1];


           Scanner sc = new Scanner(System.in);

    for(int i=0;i<emp.length;i++)
      {   // start for loop
         for(int j=0;j<emp[0].length;j++)
            {  
             emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
               if ( (emp[i][j].equals(""))) 
                 {
                   JOptionPane.showMessageDialog( null,"Employee ID Not Correct");
             emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");       
                }
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
            if(emp[i][j] == null )
                    {
                JOptionPane.showMessageDialog(null,"Employee Name Cannot Be Empty");
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
                    }
            emp[i][j] =  JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on");
            if(emp[i][j] == null )           
            {
            JOptionPane.showMessageDialog(null,"Number of Hour Cannot be empty or more than 8");
            emp[i][j] =  Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on"));
            }
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");             
            if(emp[i][j] == null)
                {
                    JOptionPane.showMessageDialog(null,"Number of Hourly Rate Cannot be empty or Minus Value");
                hourlyRate = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");                

                    } 
         emp1 = new Payroll(emp,6 ); // this is the 2D array pass to Payroll Class
                   }//end inner for loop 
       }//end for loop          

      System.out.println("Employee Id:" +emp1.getID() );
      System.out.println("Employee Name:" +emp1.getName() );
      System.out.println("Employee Hours:" +emp1.getNumOfHours());
      System.out.println("Employee Total Payment:" +emp1.gettoTotalPay() );

      }
}

is if i insert the

employe id:1
emaployee name : asd
employee num of hrs:1
employee hourly rate :100

but answer getting

Employee Id:1
Employee Name:1
Employee Hours:1
Employee Total Payment:1.0

please need advice

Upvotes: 2

Views: 387

Answers (2)

Sidharth Dash
Sidharth Dash

Reputation: 343

public Payroll(Object[][]emp, int scale)
 {
  for(int k=0;k<emp.length;k++)
  {
     this.empID = emp[k][l].toString();
     this.empName = emp[k][l].toString();
     this.numOfHours = Integer.parseInt(emp[k][l].toString()); 
     this.hourlyRate = Integer.parseInt(emp[k][l].toString());
   }

I Think you are not incrementing valraible l inside the loop. can you replace it with l++. Try and let me know....

Upvotes: 0

Sumeet
Sumeet

Reputation: 8292

Just remove the inner for loop and add

++j;

3 times in the Employer class.

Now it is working fine.The reason you are getting the nullPointerException as u mentioned in your comments is because you are not going to next columns, you are staying in column 0, for that you have to increment j

    import java.util.Scanner; 
    import javax.swing.*;
    class Employer{
             public static void main(String [] args)
      {
            String empId; 
            String empName;
            String numOfHrs;
            String hourlyRate;
            Payroll emp1 = new Payroll();
            Object [][]emp = new Object[3][4];
           Scanner sc = new Scanner(System.in);

    for(int i=0;i<emp.length;i++)
      {   // start for loop
            int j=0;  
             emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
               if ( (emp[i][j].equals(""))) 
                 {
                   JOptionPane.showMessageDialog( null,"Employee ID Not Correct");
             emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");       
                }
                ++j;
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
            if(emp[i][j] == null )
                    {
                JOptionPane.showMessageDialog(null,"Employee Name Cannot Be Empty");
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
                    }
                ++j;    
            emp[i][j] =  JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on");
            if(emp[i][j] == null )           
            {
            JOptionPane.showMessageDialog(null,"Number of Hour Cannot be empty or more than 8");
            emp[i][j] =  Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on"));
            }
                ++j;
            emp[i][j] = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");             
            if(emp[i][j] == null)
                {
                    JOptionPane.showMessageDialog(null,"Number of Hourly Rate Cannot be empty or Minus Value");
                hourlyRate = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");                

                } 
         emp1 = new Payroll(emp,i ); // this is the 2D array pass to Payroll Class
                   //end inner for loop 
       }//end for loop          
      for(int i=0;i<emp.length;i++)
      {
       System.out.println("Employee Id:" +emp1.getID() );
       System.out.println("Employee Name:" +emp1.getName() );
       System.out.println("Employee Hours:" +emp1.getNumOfHours());
       System.out.println("Employee Total Payment:" +emp1.gettoTotalPay() );
      }

      }
}
    class Payroll
    {
     private String empID;   
     private String empName;
     private int numOfHours;
     private double hourlyRate;
     private double totalPay;

     public Payroll(){}

     public Payroll(Object[][]emp, int scale)
     {

      for(int k=0;k<=scale;k++)
      {
          int l=0;
         this.empID = emp[k][l++].toString();
         this.empName = emp[k][l++].toString();
         this.numOfHours = Integer.parseInt(emp[k][l++].toString()); 
         this.hourlyRate = Float.parseFloat(emp[k][l++].toString());
       }
  }


     public String getID()
     {
     return empID;
     }
     public String getName()
     {
     return empName;
     }
     public int getNumOfHours()
     {
     return numOfHours;
     }
     public double getHourlyRate()
     {
     return hourlyRate;
     }

     public double gettoTotalPay()
     {
     return numOfHours * hourlyRate;
     }

    }

Upvotes: 1

Related Questions