Results 1 to 12 of 12
  1. #1
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Missing return statement error

    I'm new to java. Just trying to get familiar with some of the basics: classes, getter and setter methods, objects, arrays, etc.,.

    I'm trying to compile one such program but keep getting the error message: ""90:error:missing return statement". Is 90 the line it's referencing? If so, there's just a curly bracket there.

    Not sure how to proceed.

    Java Code:
    class Persons	{
    
    //Declare instance variables
    
    	private String name;
    	private String nationality;
    	private int age;
    	private int steps;
    	private int weight;
    	private int height;
    
    //Declare getter and setter methods
    
    	public String getName()	{
    		return name;
    	}
    
    
    	public String getNationality()	{
    		return nationality;
    	}
    
    
    	public int getAge()	{
    		return age;
    	}
    
    
    	public int getSteps()	{
    		return steps;
    	}
    
    
    	public int getWeight()	{
    		return weight;
    	}
    
    
    	public int getHeight()	{
    		return height;
    	}
    
    
    	public String setName(String n)	{
    		name = n;
    		return n;
    	}
    	
    	
    	public String setNationality(String z)	{
    		nationality = z;
    		return z;
    	}
    
    
    	public void setAge(int a)	{
    		age = a;
    	}
    
    
    	public void setSteps(int s)	{
    		steps = s;
    	}
    
    	public void setWeight(int w)	{
    		weight = w;
    	}
    
    	public void setHeight(int h)	{
    		height = h;
    	}
    
    	
    //Method that evaluates string value and prints applicable value
    
    	public String talk()	{
    
    	if (nationality == "American")	{
    		System.out.println("They speak English.");
    	}
    
    	else if (nationality == "German")	{
    		System.out.println("They speak German.");
    	}
    
    	else {
    		System.out.println("Unknown spoken language.");
    	}
    
          	}
    
    //Method that prints what's in the println statement the applicable number of times based on the value of steps.
    
    	public void walk()	{
    		int x = 1;
    		while (x < steps) {
    			System.out.println("I'm walking.");
    			x = x + 1;
    		}		
    	
    	}
    
    //Performs the calculation based on the weight and height values passed to it.
    	public int calcBMI()	{
    		int bmi = weight / height;
    		return bmi;
    	}
    		
    }
    
      class PersonsTestDrive	{
    
    	public static void main (String[] args)	{
    
    
    		Persons[] myHuman;
    		myHuman = new Persons[2];
    
    		myHuman[0] = new Persons();
    		
    
    		  myHuman[0].setName("Joe");
    		  myHuman[0].setNationality("American");
    		  myHuman[0].setAge(30);
    	          myHuman[0].setSteps(4);	
    		  
     			System.out.println("Person one's name:" + 						myHuman[0].getName());
    
    			System.out.println("Person one's nationality:" + 					myHuman[0].getNationality());
    		
    			System.out.println("Person one's age:" + 						myHuman[0].getAge());
    
    			myHuman[0].talk();
    			myHuman[0].walk();
    
    		myHuman[1] = new Persons();	
    
    		  myHuman[1].setName("Ingrid");
    		  myHuman[1].setNationality("German");
    		  myHuman[1].setAge(35);
    		  myHuman[1].setSteps(2);
    		  myHuman[1].setWeight(125);
    		  myHuman[1].setHeight(60);
    		
    			System.out.println("Person two's name:" + 						myHuman[1].getName());
    
    			System.out.println("Person two's nationality:" + 					myHuman[1].getNationality());
    
    			System.out.println("Person two's age:" + 						myHuman[1].getAge());
    
    			myHuman[1].talk();
    			myHuman[1].walk();
    
    			System.out.println("Person two's Body Mass 						Index is:" + myHuman[1].calcBMI());
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: Missing return statement error

    That's the closing brace to you talk method. Your talk method declares it should return a type String. Yet nothing is returned. And your string compare methods will never work. Don't use == to compare strings. Use equals().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Missing return statement error

    I used equals() in my string compare methods. I have a return statement in my setter method for nationality. Still getting the missing return statement error.

    Java Code:
    class Persons	{
    
    //Declare instance variables
    
    	private String name;
    	private String nationality;
    	private int age;
    	private int steps;
    	private int weight;
    	private int height;
    
    //Declare getter and setter methods
    
    	public String getName()	{
    		return name;
    	}
    
    
    	public String getNationality()	{
    		return nationality;
    	}
    
    
    	public int getAge()	{
    		return age;
    	}
    
    
    	public int getSteps()	{
    		return steps;
    	}
    
    
    	public int getWeight()	{
    		return weight;
    	}
    
    
    	public int getHeight()	{
    		return height;
    	}
    
    
    	public String setName(String n)	{
    		name = n;
    		return n;
    	}
    	
    	
    	public String setNationality(String z)	{
    		nationality = z;
    		return z;
    	}
    
    
    	public void setAge(int a)	{
    		age = a;
    	}
    
    
    	public void setSteps(int s)	{
    		steps = s;
    	}
    
    	public void setWeight(int w)	{
    		weight = w;
    	}
    
    	public void setHeight(int h)	{
    		height = h;
    	}
    
    	
    //Method that evaluates string value and prints applicable value
    
    	public String talk()	{
    
    	if (nationality.equals("American"))	{
    		System.out.println("They speak English.");
    	}
    
    	else if (nationality.equals("German"))	{
    		System.out.println("They speak German.");
    	}
    
    	else {
    		System.out.println("Unknown spoken language.");
    	}
    
          	}
    
    //Method that prints what's in the println statement the applicable number of times 
    
    based on the value of steps.
    
    	public void walk()	{
    		int x = 1;
    		while (x < steps) {
    			System.out.println("I'm walking.");
    			x = x + 1;
    		}		
    	
    	}
    
    //Performs the calculation based on the weight and height values passed to it.
    	public int calcBMI()	{
    		int bmi = weight / height;
    		return bmi;
    	}
    		
    }
    
      class PersonsTestDrive	{
    
    	public static void main (String[] args)	{
    
    
    		Persons[] myHuman;
    		myHuman = new Persons[2];
    
    		myHuman[0] = new Persons();
    		
    
    		  myHuman[0].setName("Joe");
    		  myHuman[0].setNationality("American");
    		  myHuman[0].setAge(30);
    	          myHuman[0].setSteps(4);	
    		  
     			System.out.println("Person one's name:" + 			
    
    			myHuman[0].getName());
    
    			System.out.println("Person one's nationality:" + 		
    
    			myHuman[0].getNationality());
    		
    			System.out.println("Person one's age:" + 			
    
    			myHuman[0].getAge());
    
    			myHuman[0].talk();
    			myHuman[0].walk();
    
    		myHuman[1] = new Persons();	
    
    		  myHuman[1].setName("Ingrid");
    		  myHuman[1].setNationality("German");
    		  myHuman[1].setAge(35);
    		  myHuman[1].setSteps(2);
    		  myHuman[1].setWeight(125);
    		  myHuman[1].setHeight(60);
    		
    			System.out.println("Person two's name:" + 			
    
    			myHuman[1].getName());
    
    			System.out.println("Person two's nationality:" + 		
    
    			myHuman[1].getNationality());
    
    			System.out.println("Person two's age:" + 			
    
    			myHuman[1].getAge());
    
    			myHuman[1].talk();
    			myHuman[1].walk();
    
    			System.out.println("Person two's Body Mass 			
    
    			Index is:" + myHuman[1].calcBMI());
    	}
    
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: Missing return statement error

    Look at lines 76 thru 90. There is no return statement.

    And line 94 needs to be commented out or removed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Missing return statement error

    still getting missing return statement error.

    Java Code:
    class Persons	{
    
    //Declare instance variables
    
    	private String name;
    	private String nationality;
    	private int age;
    	private int steps;
    	private int weight;
    	private int height;
    
    //Declare getter and setter methods
    
    	public String getName()	{
    		return name;		
    	}
    
    
    	public String getNationality()	{
    		return nationality;
    	}
    
    
    	public int getAge()	{
    		return age;
    	}
    
    
    	public int getSteps()	{
    		return steps;
    	}
    
    
    	public int getWeight()	{
    		return weight;
    	}
    
    
    	public int getHeight()	{
    		return height;
    	}
    
    
    	public String setName(String n)	{
    		name = n;
    		return n;		
    	}
    	
    	
    	public String setNationality(String z)	{
    		nationality = z;
    		return z;		
    	}
    
    
    	public void setAge(int a)	{
    		age = a;
    	}
    
    
    	public void setSteps(int s)	{
    		steps = s;
    	}
    
    	public void setWeight(int w)	{
    		weight = w;
    	}
    
    	public void setHeight(int h)	{
    		height = h;
    	}
    
    	
    //Method that evaluates string value and prints applicable value
    
    	public String talk()	{
    
    	if (nationality.equals("American"))	{
    		System.out.println("They speak English.");
    		return nationality;
    		
    	}
    
    	else if (nationality.equals("German"))	{
    		System.out.println("They speak German.");
    		return nationality;
    		
    	}
    
    	else {
    		System.out.println("Unknown spoken language.");
    	}
    
          	}
    
    //Method that prints what's in the println statement the applicable number of times 
    
    //based on the value of steps.
    
    	public void walk()	{
    		int x = 1;
    		while (x < steps) {
    			System.out.println("I'm walking.");
    			x = x + 1;
    		}		
    	
    	}
    
    //Performs the calculation based on the weight and height values passed to it.
    	public int calcBMI()	{
    		int bmi = weight / height;
    		return bmi;
    	}
    		
    }
    
      class PersonsTestDrive	{
    
    	public static void main (String[] args)	{
    
    
    		Persons[] myHuman;
    		myHuman = new Persons[2];
    
    		myHuman[0] = new Persons();
    		
    
    		  myHuman[0].setName("Joe");
    		  myHuman[0].setNationality("American");
    		  myHuman[0].setAge(30);
    	          myHuman[0].setSteps(4);	
    		  
     			System.out.println("Person one's name:" + 			
    
    			myHuman[0].getName());
    
    			System.out.println("Person one's nationality:" + 		
    
    			myHuman[0].getNationality());
    		
    			System.out.println("Person one's age:" + 			
    
    			myHuman[0].getAge());
    
    			myHuman[0].talk();
    			myHuman[0].walk();
    
    		myHuman[1] = new Persons();	
    
    		  myHuman[1].setName("Ingrid");
    		  myHuman[1].setNationality("German");
    		  myHuman[1].setAge(35);
    		  myHuman[1].setSteps(2);
    		  myHuman[1].setWeight(125);
    		  myHuman[1].setHeight(60);
    		
    			System.out.println("Person two's name:" + 			
    
    			myHuman[1].getName());
    
    			System.out.println("Person two's nationality:" + 		
    
    			myHuman[1].getNationality());
    
    			System.out.println("Person two's age:" + 			
    
    			myHuman[1].getAge());
    
    			myHuman[1].talk();
    			myHuman[1].walk();
    
    			System.out.println("Person two's Body Mass 			
    
    			Index is:" + myHuman[1].calcBMI());
    	}
    
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: Missing return statement error

    And what do you return if the first two conditionals aren't met?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Missing return statement error

    And what do you return if the first two conditionals aren't met?
    Java Code:
      else {
    		System.out.println("Unknown spoken language.");
    	}
    It's already in there.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: Missing return statement error

    I see no return statement in that else clause. Think!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Missing return statement error

    Ok, it compiled after I put the return null; statement in the else statement but I'm not understanding why I would need to have a return statement if nothing is being returned. Only if nationality = "American" and nationality="German" is anything to be returned.

    Also, look up in the section where I have \\Declare getter and setter methods The return statements are already defined there. Why put them in there again?

    When I try to run this program I'm now getting Error: main method not found in class Persons, please define main method as:

    public static void main ( String[] args )

    Why is it saying it's not found? It's in there and defined exactly as that.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: Missing return statement error

    First, every method you declare that does not have a return type of void must return whatever type you have declared. If your method does processing where it might reach the end of the method without returning anything, then that will be an error.

    Regarding main. How are you running your program? If you are trying to run Persons then the error is correct because there is no main entry point. The entry point is in the PersonsTestDrive class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Missing return statement error

    Got it.

    Thanks for your help.

  12. #12
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Missing return statement error

    You never returned a String in that method. All you have are print statements. Try changing the method type to void or adding a return statement.

Similar Threads

  1. Error: Missing Return Statement (Help!)
    By Exploit in forum New To Java
    Replies: 3
    Last Post: 07-13-2013, 01:33 PM
  2. Missing return statement error
    By Asglv in forum New To Java
    Replies: 1
    Last Post: 01-13-2013, 09:28 AM
  3. missing return statement error
    By Joshkesner in forum New To Java
    Replies: 2
    Last Post: 10-21-2012, 06:38 AM
  4. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •