469,903 Members | 1,902 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

missing return statemnet error

sammyboy78
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and pay rate.
I get an error when trying to compile WeeklyPay.java:

WeeklyPay.java:78: missing return statement
}//end displayPay
^

Also if anyone sees anything else that's going to give me a problem please tell me about it. Thanks!

here is my code for WeeklyPay.java:

Expand|Select|Wrap|Line Numbers
  1. // WeeklyPay.java
  2. // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
  3. import java.util.Scanner;
  4.  
  5. public class WeeklyPay
  6. {
  7.  
  8.     String empName;
  9.     double hours;
  10.     double rate;
  11.     double pay;
  12.  
  13.     Scanner input = new Scanner( System.in );
  14.  
  15.     public void enterEmployeeName()
  16.     {
  17.         String empName; //employee name entered by user
  18.  
  19.         System.out.println(); // prints blank line
  20.         System.out.println( "Please enter employee name, enter stop to quit:");
  21.         empName = input.nextLine(); //read employee name from user
  22.         System.out.println(); // prints blank line
  23.  
  24.     }// end enterEmployeeName
  25.  
  26.     public String getName ()
  27.     {
  28.         return empName;
  29.  
  30.     }//end getName
  31.  
  32.     public void enterHours()
  33.     {
  34.         System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  35.         hours = input.nextDouble(); //read hours worked
  36.         System.out.println(); // prints blank line
  37.  
  38.             while ( hours < 0.00 ) // loop if amount is negative
  39.             {
  40.                 System.out.println(); // prints blank line
  41.                 System.out.println( "Error! Please enter positive number!" );
  42.                 System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  43.                 hours = input.nextDouble(); //read hours worked
  44.                 System.out.println(); // prints blank line
  45.             } // end while
  46.  
  47.     }// end enterHours
  48.  
  49.     public void enterRate()
  50.     {
  51.         System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  52.         rate = input.nextDouble();//read rate of pay
  53.         System.out.println();
  54.  
  55.             while ( rate < 0.00 ) // loop if amount is negative
  56.             {
  57.                 System.out.println(); // prints blank line
  58.                 System.out.println( "Error! Please enter a positive number!" );
  59.                 System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  60.                 rate = input.nextDouble();//read rate of pay
  61.                 System.out.println();
  62.             } // end while
  63.  
  64.     }// end enterRate
  65.  
  66.     private void setPay()
  67.     {
  68.         pay = rate * hours;
  69.  
  70.     }//end setPay
  71.  
  72.     public String displayPay()
  73.     {
  74.         System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
  75.         System.out.println();
  76.         input.nextLine();
  77.  
  78.     }//end displayPay
  79.  
  80.  }// end class WeeklyPay
here is my code for WeeklyPayTest:

Expand|Select|Wrap|Line Numbers
  1. //WeeklyPayTest.java
  2. //Creat and manipulate a WeeklyPay object
  3. import java.util.Scanner; //program uses Scanner
  4.  
  5. public class WeeklyPayTest
  6. {
  7.     //main method begins program execution
  8.     public static void main( String args [] )
  9.     {
  10.         //create Scanner to obtain input
  11.         Scanner input = new Scanner(System.in );
  12.  
  13.         //create a WeeklyPay object and assign to payroll
  14.         WeeklyPay payroll = new WeeklyPay();
  15.  
  16.         boolean condition = true
  17.  
  18.         while ( condition )
  19.         {
  20.             payroll.enterEmployeeName( input.nextLine()); // call method to input employee name
  21.  
  22.             if( ! (payroll.getName().equals("stop")))
  23.             {
  24.                 payroll.enterHours( input.nextDouble() ); //call method to input hours worked
  25.                 payroll.enterRate( input.nextDouble() ); // call method to input pay rate
  26.                 payroll.displayPay(); //calls method to display employee name and weekly pay amount
  27.  
  28.             }//end if
  29.  
  30.             else
  31.             {
  32.                 condition = false;
  33.  
  34.             }//end else
  35.  
  36.         }//end while
  37.  
  38.  
  39.     }//end main
  40.  
  41. }//end class WeeklyPayTest
Jun 10 '07 #1
8 1696
JosAH
11,448 Expert 8TB
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and pay rate.
I get an error when trying to compile WeeklyPay.java:

WeeklyPay.java:78: missing return statement
}//end displayPay
^

Also if anyone sees anything else that's going to give me a problem please tell me about it. Thanks!

here is my code for WeeklyPay.java:

Expand|Select|Wrap|Line Numbers
  1.     public String displayPay()
  2.     {
  3.         System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
  4.         System.out.println();
  5.         input.nextLine();
  6.  
  7.     }//end displayPay
  8.  
You declare the return type of that method as a String; nowhere in that little
method do you return a String; that's why the compiler is whining at you.

kind regards,

Jos
Jun 10 '07 #2
r035198x
13,262 8TB
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and pay rate.
I get an error when trying to compile WeeklyPay.java:

WeeklyPay.java:78: missing return statement
}//end displayPay
^

Also if anyone sees anything else that's going to give me a problem please tell me about it. Thanks!

here is my code for WeeklyPay.java:

Expand|Select|Wrap|Line Numbers
  1. // WeeklyPay.java
  2. // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
  3. import java.util.Scanner;
  4.  
  5. public class WeeklyPay
  6. {
  7.  
  8.     String empName;
  9.     double hours;
  10.     double rate;
  11.     double pay;
  12.  
  13.     Scanner input = new Scanner( System.in );
  14.  
  15.     public void enterEmployeeName()
  16.     {
  17.         String empName; //employee name entered by user
  18.  
  19.         System.out.println(); // prints blank line
  20.         System.out.println( "Please enter employee name, enter stop to quit:");
  21.         empName = input.nextLine(); //read employee name from user
  22.         System.out.println(); // prints blank line
  23.  
  24.     }// end enterEmployeeName
  25.  
  26.     public String getName ()
  27.     {
  28.         return empName;
  29.  
  30.     }//end getName
  31.  
  32.     public void enterHours()
  33.     {
  34.         System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  35.         hours = input.nextDouble(); //read hours worked
  36.         System.out.println(); // prints blank line
  37.  
  38.             while ( hours < 0.00 ) // loop if amount is negative
  39.             {
  40.                 System.out.println(); // prints blank line
  41.                 System.out.println( "Error! Please enter positive number!" );
  42.                 System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  43.                 hours = input.nextDouble(); //read hours worked
  44.                 System.out.println(); // prints blank line
  45.             } // end while
  46.  
  47.     }// end enterHours
  48.  
  49.     public void enterRate()
  50.     {
  51.         System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  52.         rate = input.nextDouble();//read rate of pay
  53.         System.out.println();
  54.  
  55.             while ( rate < 0.00 ) // loop if amount is negative
  56.             {
  57.                 System.out.println(); // prints blank line
  58.                 System.out.println( "Error! Please enter a positive number!" );
  59.                 System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  60.                 rate = input.nextDouble();//read rate of pay
  61.                 System.out.println();
  62.             } // end while
  63.  
  64.     }// end enterRate
  65.  
  66.     private void setPay()
  67.     {
  68.         pay = rate * hours;
  69.  
  70.     }//end setPay
  71.  
  72.     public String displayPay()
  73.     {
  74.         System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
  75.         System.out.println();
  76.         input.nextLine();
  77.  
  78.     }//end displayPay
  79.  
  80. }// end class WeeklyPay
here is my code for WeeklyPayTest:

Expand|Select|Wrap|Line Numbers
  1. //WeeklyPayTest.java
  2. //Creat and manipulate a WeeklyPay object
  3. import java.util.Scanner; //program uses Scanner
  4.  
  5. public class WeeklyPayTest
  6. {
  7.     //main method begins program execution
  8.     public static void main( String args [] )
  9.     {
  10.         //create Scanner to obtain input
  11.         Scanner input = new Scanner(System.in );
  12.  
  13.         //create a WeeklyPay object and assign to payroll
  14.         WeeklyPay payroll = new WeeklyPay();
  15.  
  16.         boolean condition = true
  17.  
  18.         while ( condition )
  19.         {
  20.             payroll.enterEmployeeName( input.nextLine()); // call method to input employee name
  21.  
  22.             if( ! (payroll.getName().equals("stop")))
  23.             {
  24.                 payroll.enterHours( input.nextDouble() ); //call method to input hours worked
  25.                 payroll.enterRate( input.nextDouble() ); // call method to input pay rate
  26.                 payroll.displayPay(); //calls method to display employee name and weekly pay amount
  27.  
  28.             }//end if
  29.  
  30.             else
  31.             {
  32.                 condition = false;
  33.  
  34.             }//end else
  35.  
  36.         }//end while
  37.  
  38.  
  39.     }//end main
  40.  
  41. }//end class WeeklyPayTest
Your displayPay method signature says that it returns a String but it does not do that. The compiler therefore has to complain.
Jun 10 '07 #3
ok I changed that return type to void and it compiled along with my WEeklyPayTest.java. Now when I try to run the WeeklyPayTest I get this error:

Please enter employee name, enter stop to quit:
Sam

Exception in thread "main" java.lang.NullPointerException
at WeeklyPayTest.main(WeeklyPayTest.java:23)

Can anyone tell me what I'm doing wrong? Here's my code again:

Expand|Select|Wrap|Line Numbers
  1. //WeeklyPayTest.java
  2. //Creat and manipulate a WeeklyPay object
  3. import java.util.Scanner; //program uses Scanner
  4.  
  5. public class WeeklyPayTest
  6. {
  7.  
  8.         //main method begins program execution
  9.     public static void main( String args [] )
  10.     {
  11.         //create Scanner to obtain input
  12.         Scanner input = new Scanner(System.in );
  13.  
  14.         //create a WeeklyPay object and assign to payroll
  15.         WeeklyPay payroll = new WeeklyPay();
  16.  
  17.         boolean condition = true;
  18.  
  19.         while ( condition )
  20.         {
  21.             payroll.employeeName(); // call method to input employee name
  22.  
  23.             if( ! (payroll.getName().equals("stop")))
  24.             {
  25.                 payroll.hours(); //call method to input hours worked
  26.                 payroll.rate(); // call method to input pay rate
  27.                 payroll.displayPay(); //calls method to display employee name and weekly pay amount
  28.  
  29.             }//end if
  30.  
  31.             else
  32.             {
  33.                 condition = false;
  34.  
  35.             }//end else
  36.  
  37.         }//end while
  38.  
  39.  
  40.     }//end main
  41.  
  42. }//end class WeeklyPayTest

Expand|Select|Wrap|Line Numbers
  1. // WeeklyPay.java
  2. // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
  3. import java.util.Scanner;
  4.  
  5. public class WeeklyPay
  6. {
  7.  
  8.     String empName;
  9.     double hours;
  10.     double rate;
  11.     double pay;
  12.  
  13.     Scanner input = new Scanner( System.in );
  14.  
  15.     public void employeeName()
  16.     {
  17.         String empName; //employee name entered by user
  18.  
  19.         System.out.println(); // prints blank line
  20.         System.out.println( "Please enter employee name, enter stop to quit:");
  21.         empName = input.nextLine(); //read employee name from user
  22.         System.out.println(); // prints blank line
  23.  
  24.     }// end enterEmployeeName
  25.  
  26.     public String getName ()
  27.     {
  28.         return empName;
  29.  
  30.     }//end getName
  31.  
  32.     public void hours()
  33.     {
  34.         System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  35.         hours = input.nextDouble(); //read hours worked
  36.         System.out.println(); // prints blank line
  37.  
  38.             while ( hours < 0.00 ) // loop if amount is negative
  39.             {
  40.                 System.out.println(); // prints blank line
  41.                 System.out.println( "Error! Please enter positive number!" );
  42.                 System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  43.                 hours = input.nextDouble(); //read hours worked
  44.                 System.out.println(); // prints blank line
  45.             } // end while
  46.  
  47.     }// end enterHours
  48.  
  49.     public void rate()
  50.     {
  51.         System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  52.         rate = input.nextDouble();//read rate of pay
  53.         System.out.println();
  54.  
  55.             while ( rate < 0.00 ) // loop if amount is negative
  56.             {
  57.                 System.out.println(); // prints blank line
  58.                 System.out.println( "Error! Please enter a positive number!" );
  59.                 System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  60.                 rate = input.nextDouble();//read rate of pay
  61.                 System.out.println();
  62.             } // end while
  63.  
  64.     }// end enterRate
  65.  
  66.     private void setPay()
  67.     {
  68.         pay = rate * hours;
  69.  
  70.     }//end setPay
  71.  
  72.     public void displayPay()
  73.     {
  74.         System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
  75.         System.out.println();
  76.         input.nextLine();
  77.  
  78.     }//end displayPay
  79.  
  80.  }// end class WeeklyPay
Jun 10 '07 #4
JosAH
11,448 Expert 8TB
In your employeeName() method your create a local String empName; that one
will be set and your member variable with the same name will still remain null.

kind regards,

Jos
Jun 10 '07 #5
Thanks!

Now my problem is that when I run WeeklyPayTest it won't calculate my hours and rate. It keeps returning $0.00 no matter what amounts I enter:

C:\Documents and Settings\Sam>java WeeklyPayTest

Please enter employee name, enter stop to quit: Sam

Enter hours worked by Sam : 45

Enter rate of pay for Sam :$45

Employee name: Sam
Weekly pay amount: $0.00

Please enter employee name, enter stop to quit: stop


C:\Documents and Settings\Sam>


What do I need to do to get it to work like I need it to?

Expand|Select|Wrap|Line Numbers
  1. // WeeklyPay.java
  2. // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
  3. import java.util.Scanner;
  4.  
  5. public class WeeklyPay
  6. {
  7.  
  8.     String empName;
  9.     double hours;
  10.     double rate;
  11.     double pay;
  12.  
  13.     Scanner input = new Scanner( System.in );
  14.  
  15.     public void employeeName()
  16.     {
  17.  
  18.         System.out.println(); // prints blank line
  19.         System.out.printf( "Please enter employee name, enter stop to quit: ");
  20.         empName = input.nextLine(); //read employee name from user
  21.         System.out.println(); // prints blank line
  22.  
  23.     }// end enterEmployeeName
  24.  
  25.     public String getName ()
  26.     {
  27.         return empName;
  28.  
  29.     }//end getName
  30.  
  31.     public void hours()
  32.     {
  33.         System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  34.         hours = input.nextDouble(); //read hours worked
  35.         System.out.println(); // prints blank line
  36.  
  37.             while ( hours < 0.00 ) // loop if amount is negative
  38.             {
  39.                 System.out.println(); // prints blank line
  40.                 System.out.println( "Error! Please enter positive number!" );
  41.                 System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
  42.                 hours = input.nextDouble(); //read hours worked
  43.                 System.out.println(); // prints blank line
  44.             } // end while
  45.  
  46.     }// end hours
  47.  
  48.     public void rate()
  49.     {
  50.         System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  51.         rate = input.nextDouble();//read rate of pay
  52.         System.out.println();
  53.  
  54.             while ( rate < 0.00 ) // loop if amount is negative
  55.             {
  56.                 System.out.println(); // prints blank line
  57.                 System.out.println( "Error! Please enter a positive number!" );
  58.                 System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
  59.                 rate = input.nextDouble();//read rate of pay
  60.                 System.out.println();
  61.             } // end while
  62.  
  63.     }// end rate
  64.  
  65.     public void setPay()
  66.     {
  67.         pay = rate * hours; //calculate pay by multiplying hours and rate
  68.  
  69.     }//end setPay
  70.  
  71.     public double getPay()
  72.     {
  73.         return pay;
  74.  
  75.     }// end getPay
  76.  
  77.  
  78.     public void displayPay()
  79.     {
  80.         System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2f", empName, getPay() ); //display name and pay amount
  81.         System.out.println();
  82.         input.nextLine();
  83.  
  84.     }//end displayPay
  85.  
  86.  }// end class WeeklyPay
Expand|Select|Wrap|Line Numbers
  1. //WeeklyPayTest.java
  2. //Creat and manipulate a WeeklyPay object
  3. import java.util.Scanner; //program uses Scanner
  4.  
  5. public class WeeklyPayTest
  6. {
  7.  
  8.         //main method begins program execution
  9.     public static void main( String args [] )
  10.     {
  11.         //create Scanner to obtain input
  12.         Scanner input = new Scanner(System.in );
  13.  
  14.         //create a WeeklyPay object and assign to payroll
  15.         WeeklyPay payroll = new WeeklyPay();
  16.  
  17.         boolean condition = true;
  18.  
  19.         while ( condition )
  20.         {
  21.             payroll.employeeName(); // call method to input employee name
  22.  
  23.             if( ! (payroll.getName().equals("stop")))
  24.             {
  25.                 payroll.hours(); //call method to input hours worked
  26.                 payroll.rate(); // call method to input pay rate
  27.                 payroll.displayPay(); //calls method to display employee name and weekly pay amount
  28.  
  29.             }//end if
  30.  
  31.             else
  32.             {
  33.                 condition = false;
  34.  
  35.             }//end else
  36.  
  37.         }//end while
  38.  
  39.  
  40.     }//end main
  41.  
  42. }//end class WeeklyPayTest
Jun 10 '07 #6
JosAH
11,448 Expert 8TB
I never see your setPay() method being called anywhere in your code. That
method calculates the amount of money to be paid. Or maybe I'm just blind ;-)

kind regards,

Jos
Jun 10 '07 #7
sweet! I overlooked that. Now it works perfectly! Thanks for helping this java newbie out. I appreciate it.
Jun 10 '07 #8
JosAH
11,448 Expert 8TB
sweet! I overlooked that. Now it works perfectly! Thanks for helping this java newbie out. I appreciate it.
Thanks; no problem; we're nice folks here actually as long as one shows that
s/he's been trying to solve some problem(s) him/herself first.If one gets stuck
then we'll help you out. OTOH if one is just too lazy to put some effort in at first,
we (read: I) are/am too lazy too to do any homework for others at all ;-)

You showed that you've put in a lot of work in it yourself so we'll help you out.
Have fun studying the Java programming language.


kind regards,

Jos
Jun 10 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by soup_or_power | last post: by
reply views Thread by Jigar.Patel | last post: by
3 posts views Thread by Fred Chateau | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.