472,780 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 1801
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

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

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
2
by: Sonoman | last post by:
Hi all: I am getting a "missing storage class or idetifier" error and I do not understand what it means, therefore and I cannot figure it out. I am just starting a new project and I cannot get...
1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
1
by: soup_or_power | last post by:
I'm passing the return from window.open as a function argument and getting the error "missing ] after element list" when tested with FireFox. Here is the relevant code. Many thanks for your help....
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project by serveraddresss?wsdl, it gives me following error: Custom tool...
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project, it gives me following error: Custom tool error: Unable to import...
3
by: Fred Chateau | last post by:
Still working on my XML DataSet... Having moved on past difficult and complex problems, resolved with the assistance of everyone here, I find myself facing yet another problem. My XML document...
11
by: honguin | last post by:
Hi, With the following code, I have created a web request to a url which I am making a HTML POST with the html page request.htm, even though it makes a HTML POST, the StreamReader produces a XML...
2
by: im2cre8iv | last post by:
Here is my code where I am receiving the error: #include <fstream> #include "BinaryTree.h" using namespace std; Node* BinaryTree::MakeTree(ifstream& infile) { char name; infile>>name;
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.