missing return statemnet error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sammyboy78
    New Member
    • Jun 2007
    • 21

    missing return statemnet error

    I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayT est" 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:

    Code:
    // WeeklyPay.java
    // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
    import java.util.Scanner;
    
    public class WeeklyPay
    {
    
    	String empName;
    	double hours;
    	double rate;
    	double pay;
    
    	Scanner input = new Scanner( System.in );
    
    	public void enterEmployeeName()
    	{
    		String empName; //employee name entered by user
    
    		System.out.println(); // prints blank line
    		System.out.println( "Please enter employee name, enter stop to quit:");
    		empName = input.nextLine(); //read employee name from user
    		System.out.println(); // prints blank line
    
    	}// end enterEmployeeName
    
    	public String getName ()
    	{
    		return empName;
    
    	}//end getName
    
    	public void enterHours()
    	{
    		System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
    		hours = input.nextDouble(); //read hours worked
    		System.out.println(); // prints blank line
    
    			while ( hours < 0.00 ) // loop if amount is negative
    			{
    				System.out.println(); // prints blank line
    				System.out.println( "Error! Please enter positive number!" );
    				System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
    				hours = input.nextDouble(); //read hours worked
    				System.out.println(); // prints blank line
    			} // end while
    
    	}// end enterHours
    
    	public void enterRate()
    	{
    		System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
    		rate = input.nextDouble();//read rate of pay
    		System.out.println();
    
    			while ( rate < 0.00 ) // loop if amount is negative
    			{
    				System.out.println(); // prints blank line
    				System.out.println( "Error! Please enter a positive number!" );
    				System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
    				rate = input.nextDouble();//read rate of pay
    				System.out.println();
    			} // end while
    
    	}// end enterRate
    
    	private void setPay()
    	{
    		pay = rate * hours;
    
    	}//end setPay
    
    	public String displayPay()
    	{
    		System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
    		System.out.println();
    		input.nextLine();
    
    	}//end displayPay
    
     }// end class WeeklyPay
    here is my code for WeeklyPayTest:

    Code:
    //WeeklyPayTest.java
    //Creat and manipulate a WeeklyPay object
    import java.util.Scanner; //program uses Scanner
    
    public class WeeklyPayTest
    {
    	//main method begins program execution
    	public static void main( String args [] )
    	{
    		//create Scanner to obtain input
    		Scanner input = new Scanner(System.in );
    
    		//create a WeeklyPay object and assign to payroll
    		WeeklyPay payroll = new WeeklyPay();
    
    		boolean condition = true
    
    		while ( condition )
    		{
    			payroll.enterEmployeeName( input.nextLine()); // call method to input employee name
    
    			if( ! (payroll.getName().equals("stop")))
    			{
    				payroll.enterHours( input.nextDouble() ); //call method to input hours worked
    				payroll.enterRate( input.nextDouble() ); // call method to input pay rate
    				payroll.displayPay(); //calls method to display employee name and weekly pay amount
    
    			}//end if
    
    			else
    			{
    				condition = false;
    
    			}//end else
    
    		}//end while
    
    
    	}//end main
    
    }//end class WeeklyPayTest
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by sammyboy78
    I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayT est" 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:

    [CODE=java] public String displayPay()
    {
    System.out.prin tf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
    System.out.prin tln();
    input.nextLine( );

    }//end displayPay
    [/CODE]
    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

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by sammyboy78
      I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayT est" 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:

      Code:
      // WeeklyPay.java
      // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
      import java.util.Scanner;
       
      public class WeeklyPay
      {
       
      	String empName;
      	double hours;
      	double rate;
      	double pay;
       
      	Scanner input = new Scanner( System.in );
       
      	public void enterEmployeeName()
      	{
      		String empName; //employee name entered by user
       
      		System.out.println(); // prints blank line
      		System.out.println( "Please enter employee name, enter stop to quit:");
      		empName = input.nextLine(); //read employee name from user
      		System.out.println(); // prints blank line
       
      	}// end enterEmployeeName
       
      	public String getName ()
      	{
      		return empName;
       
      	}//end getName
       
      	public void enterHours()
      	{
      		System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
      		hours = input.nextDouble(); //read hours worked
      		System.out.println(); // prints blank line
       
      			while ( hours < 0.00 ) // loop if amount is negative
      			{
      				System.out.println(); // prints blank line
      				System.out.println( "Error! Please enter positive number!" );
      				System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
      				hours = input.nextDouble(); //read hours worked
      				System.out.println(); // prints blank line
      			} // end while
       
      	}// end enterHours
       
      	public void enterRate()
      	{
      		System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
      		rate = input.nextDouble();//read rate of pay
      		System.out.println();
       
      			while ( rate < 0.00 ) // loop if amount is negative
      			{
      				System.out.println(); // prints blank line
      				System.out.println( "Error! Please enter a positive number!" );
      				System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
      				rate = input.nextDouble();//read rate of pay
      				System.out.println();
      			} // end while
       
      	}// end enterRate
       
      	private void setPay()
      	{
      		pay = rate * hours;
       
      	}//end setPay
       
      	public String displayPay()
      	{
      		System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
      		System.out.println();
      		input.nextLine();
       
      	}//end displayPay
       
      }// end class WeeklyPay
      here is my code for WeeklyPayTest:

      Code:
      //WeeklyPayTest.java
      //Creat and manipulate a WeeklyPay object
      import java.util.Scanner; //program uses Scanner
       
      public class WeeklyPayTest
      {
      	//main method begins program execution
      	public static void main( String args [] )
      	{
      		//create Scanner to obtain input
      		Scanner input = new Scanner(System.in );
       
      		//create a WeeklyPay object and assign to payroll
      		WeeklyPay payroll = new WeeklyPay();
       
      		boolean condition = true
       
      		while ( condition )
      		{
      			payroll.enterEmployeeName( input.nextLine()); // call method to input employee name
       
      			if( ! (payroll.getName().equals("stop")))
      			{
      				payroll.enterHours( input.nextDouble() ); //call method to input hours worked
      				payroll.enterRate( input.nextDouble() ); // call method to input pay rate
      				payroll.displayPay(); //calls method to display employee name and weekly pay amount
       
      			}//end if
       
      			else
      			{
      				condition = false;
       
      			}//end else
       
      		}//end while
       
       
      	}//end main
       
      }//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.

      Comment

      • sammyboy78
        New Member
        • Jun 2007
        • 21

        #4
        ok I changed that return type to void and it compiled along with my WEeklyPayTest.j ava. 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.NullP ointerException
        at WeeklyPayTest.m ain(WeeklyPayTe st.java:23)

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

        Code:
        //WeeklyPayTest.java
        //Creat and manipulate a WeeklyPay object
        import java.util.Scanner; //program uses Scanner
        
        public class WeeklyPayTest
        {
        
        		//main method begins program execution
        	public static void main( String args [] )
        	{
        		//create Scanner to obtain input
        		Scanner input = new Scanner(System.in );
        
        		//create a WeeklyPay object and assign to payroll
        		WeeklyPay payroll = new WeeklyPay();
        
        		boolean condition = true;
        
        		while ( condition )
        		{
        			payroll.employeeName(); // call method to input employee name
        
        			if( ! (payroll.getName().equals("stop")))
        			{
        				payroll.hours(); //call method to input hours worked
        				payroll.rate(); // call method to input pay rate
        				payroll.displayPay(); //calls method to display employee name and weekly pay amount
        
        			}//end if
        
        			else
        			{
        				condition = false;
        
        			}//end else
        
        		}//end while
        
        
        	}//end main
        
        }//end class WeeklyPayTest

        Code:
        // WeeklyPay.java
        // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
        import java.util.Scanner;
        
        public class WeeklyPay
        {
        
        	String empName;
        	double hours;
        	double rate;
        	double pay;
        
        	Scanner input = new Scanner( System.in );
        
        	public void employeeName()
        	{
        		String empName; //employee name entered by user
        
        		System.out.println(); // prints blank line
        		System.out.println( "Please enter employee name, enter stop to quit:");
        		empName = input.nextLine(); //read employee name from user
        		System.out.println(); // prints blank line
        
        	}// end enterEmployeeName
        
        	public String getName ()
        	{
        		return empName;
        
        	}//end getName
        
        	public void hours()
        	{
        		System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
        		hours = input.nextDouble(); //read hours worked
        		System.out.println(); // prints blank line
        
        			while ( hours < 0.00 ) // loop if amount is negative
        			{
        				System.out.println(); // prints blank line
        				System.out.println( "Error! Please enter positive number!" );
        				System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
        				hours = input.nextDouble(); //read hours worked
        				System.out.println(); // prints blank line
        			} // end while
        
        	}// end enterHours
        
        	public void rate()
        	{
        		System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
        		rate = input.nextDouble();//read rate of pay
        		System.out.println();
        
        			while ( rate < 0.00 ) // loop if amount is negative
        			{
        				System.out.println(); // prints blank line
        				System.out.println( "Error! Please enter a positive number!" );
        				System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
        				rate = input.nextDouble();//read rate of pay
        				System.out.println();
        			} // end while
        
        	}// end enterRate
        
        	private void setPay()
        	{
        		pay = rate * hours;
        
        	}//end setPay
        
        	public void displayPay()
        	{
        		System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2d", empName, pay); //display name and pay amount
        		System.out.println();
        		input.nextLine();
        
        	}//end displayPay
        
         }// end class WeeklyPay

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          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

          Comment

          • sammyboy78
            New Member
            • Jun 2007
            • 21

            #6
            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>ja va 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?

            Code:
            // WeeklyPay.java
            // This program calculates the weekly pay when the user inputs the employee name, rate of pay and hours worked.
            import java.util.Scanner;
            
            public class WeeklyPay
            {
            
            	String empName;
            	double hours;
            	double rate;
            	double pay;
            
            	Scanner input = new Scanner( System.in );
            
            	public void employeeName()
            	{
            
            		System.out.println(); // prints blank line
            		System.out.printf( "Please enter employee name, enter stop to quit: ");
            		empName = input.nextLine(); //read employee name from user
            		System.out.println(); // prints blank line
            
            	}// end enterEmployeeName
            
            	public String getName ()
            	{
            		return empName;
            
            	}//end getName
            
            	public void hours()
            	{
            		System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
            		hours = input.nextDouble(); //read hours worked
            		System.out.println(); // prints blank line
            
            			while ( hours < 0.00 ) // loop if amount is negative
            			{
            				System.out.println(); // prints blank line
            				System.out.println( "Error! Please enter positive number!" );
            				System.out.printf( "Enter hours worked by %s : ", empName ); //prompt
            				hours = input.nextDouble(); //read hours worked
            				System.out.println(); // prints blank line
            			} // end while
            
            	}// end hours
            
            	public void rate()
            	{
            		System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
            		rate = input.nextDouble();//read rate of pay
            		System.out.println();
            
            			while ( rate < 0.00 ) // loop if amount is negative
            			{
            				System.out.println(); // prints blank line
            				System.out.println( "Error! Please enter a positive number!" );
            				System.out.printf( "Enter rate of pay for %s :$", empName ); //prompt
            				rate = input.nextDouble();//read rate of pay
            				System.out.println();
            			} // end while
            
            	}// end rate
            
            	public void setPay()
            	{
            		pay = rate * hours; //calculate pay by multiplying hours and rate
            
            	}//end setPay
            
            	public double getPay()
            	{
            		return pay;
            
            	}// end getPay
            
            
            	public void displayPay()
            	{
            		System.out.printf( "Employee name: %s \nWeekly pay amount: $%.2f", empName, getPay() ); //display name and pay amount
            		System.out.println();
            		input.nextLine();
            
            	}//end displayPay
            
             }// end class WeeklyPay
            Code:
            //WeeklyPayTest.java
            //Creat and manipulate a WeeklyPay object
            import java.util.Scanner; //program uses Scanner
            
            public class WeeklyPayTest
            {
            
            		//main method begins program execution
            	public static void main( String args [] )
            	{
            		//create Scanner to obtain input
            		Scanner input = new Scanner(System.in );
            
            		//create a WeeklyPay object and assign to payroll
            		WeeklyPay payroll = new WeeklyPay();
            
            		boolean condition = true;
            
            		while ( condition )
            		{
            			payroll.employeeName(); // call method to input employee name
            
            			if( ! (payroll.getName().equals("stop")))
            			{
            				payroll.hours(); //call method to input hours worked
            				payroll.rate(); // call method to input pay rate
            				payroll.displayPay(); //calls method to display employee name and weekly pay amount
            
            			}//end if
            
            			else
            			{
            				condition = false;
            
            			}//end else
            
            		}//end while
            
            
            	}//end main
            
            }//end class WeeklyPayTest

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

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

              Comment

              • sammyboy78
                New Member
                • Jun 2007
                • 21

                #8
                sweet! I overlooked that. Now it works perfectly! Thanks for helping this java newbie out. I appreciate it.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by sammyboy78
                  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

                  Comment

                  Working...