-4

The weekly earnings is not showing first 3 times. It's declaring as size of 4. Weekly earnings is calculating 4 times but it's not displaying. Only 4th time it's displaying.

    package test;

    import java.util.*;

    public class test {
        public static void main(String[] args) {
            String Name[]= new String[4];
            double hoursWorked[] =new double[4];
            double hourlyPay[] = new double[4];
            double WeeklyEarnings[] =new double[4];


            GetInfo(Name,hoursWorked,hourlyPay);
            WeeklyEarnings[3] = CalculateTotal(hoursWorked,hourlyPay);
            DisplayInfo(Name,hoursWorked,hourlyPay,WeeklyEarnings); 
        }

         public static void GetInfo(String Name[], double hoursWorked[], double hourlyPay[])
         {

             String blank;
             Scanner UserIn = new Scanner(System.in);
             for(int i=0;i< Name.length;i++)
             {


                 System.out.print("Please enter name #"+(i+1) + ":");
                 Name[i]= UserIn.nextLine();


                 do
                 {
                 System.out.print("Please enter the number of hours worked:");
                 hoursWorked[i] = UserIn.nextDouble();
                 if(hoursWorked[i]<0)
                 {
                     System.out.print("Invalid entry !!! Please try again");

                 }
                 }while(hoursWorked[i]<0);

                 System.out.print("Please enter the hourly pay rate: ");
                 hourlyPay[i]= UserIn.nextDouble();

                 blank= UserIn.nextLine();
                 System.out.print("\n");

             } 
         }

         public static double CalculateTotal(double hoursWorked[], double hourlyPay[])
            {
                double[] WeeklyEarnings =new double[4];

                for(int i=0;i<hoursWorked.length;i++)
                {
                WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

                }


                return WeeklyEarnings[3];

            }


     public static void DisplayInfo(String Name[], double hoursWorked[], double hourlyPay[], double weeklyEarnings[])
             {

    System.out.print("\nName\t Hours Worked\t Hourly Pay\t Weekly Earnings");

    for(int i=0;i<weeklyEarnings.length;i++)
    {
        System.out.printf("\n"+ Name[i]+"\t"     +hoursWorked[i] +"\t\t"  +"$"     +hourlyPay[i] +"\t\t" +     "$%.2f",weeklyEarnings[i]);


    }


             }




         } 
halfer
  • 19,824
  • 17
  • 99
  • 186
JavaNew
  • 11
  • 8
  • This is extremely vague. You're giving us no context/details about your program and the problem you're encountering. Additionally you're just making a broad request for us to debug your program for you. You debug it, find a specific problem and ask a specific answerable question. – tnw Apr 19 '17 at 17:13
  • [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Apr 19 '17 at 17:21

2 Answers2

1
  public static double CalculateTotal(double hoursWorked[], double hourlyPay[])
        {
            double[] WeeklyEarnings =new double[4];

            for(int i=0;i<hoursWorked.length;i++)
            {
            WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

            }


            return WeeklyEarnings[3];

        }

what you are doing here is returing only one element of the array ( the fourth) instead you should return all the array and manipulate it

     public static double[] CalculateTotal(double hoursWorked[], double hourlyPay[])
        {
            double[] WeeklyEarnings =new double[4];

            for(int i=0;i<hoursWorked.length;i++)
            {
            WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

            }


            return WeeklyEarnings;

        }
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • Thank You so much !! there is no error showing .. I am trying to calculate weekly earnings for 4 employees by hourlyPay * Hoursworked in a method named CalculateTotal(). for 1st 3 employees ,weekly earnings showing as 0 . only for 4th employee the calculation is showing. output as showing below. Please help.Thank You Name Hours Worked Hourly Pay Weekly Earnings ddd 4.0 $5.0 $0.00 sdd 5.0 $6.0 $0.00 eee 4.0 $3.0 $0.00 ddd 4.0 $5.0 $20.00 – JavaNew Apr 19 '17 at 18:00
  • tried return weeklyEarnings; but it showing error ------cannot convert from double[] to double – JavaNew Apr 19 '17 at 18:02
  • yes you need to adapt your code to receeve array instead of single value and manipulate it, for example instead of WeeklyEarnings[3] = CalculateTotal(hoursWorked,hourlyPay) , you need it to be , WeeklyEarnings = CalculateTotal(hoursWorked,hourlyPay) and so on, please try to read more about java – Amer Qarabsa Apr 19 '17 at 18:31
0

Change return WeeklyEarnings[3] to return WeeklyEarnings[]

You asked the method to return only the 4th index instead of returning all indexes of the array.

plooms
  • 93
  • 2
  • 11
  • Thank You !!! tried return weeklyEarnings; but it showing error ------cannot convert from double[] to double – JavaNew Apr 19 '17 at 18:02
  • Sorry, check edit, I added empty brackets to WeeklyEarnings so Java knows it's an array. This should work but I'm a beginner too so I'm not positive. Also, I think it's the convention to have the first word of a variable in lowercase, i.e. weeklyEarnings, as capitalizing every word is reserved for making classes. – plooms Apr 19 '17 at 18:30
  • 1
    Code-only answers do very little to educate SO readers. Your answer may or may not be a correct answer, but it is in the moderation queue after being marked as "low-quality". Please take a moment to improve your answer with an explanation. (Do not use comments to explain your answer.) – mickmackusa Apr 19 '17 at 20:22