1,105,332 Community Members

No return statement-Simple

Member Avatar
xiangzhuang
Newbie Poster
6 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey guys I keep getting the "no return statement error in two places, I have commented them for you

/**
 * A WordsCompare object stores Strings.  It is used to determine if the first word 
 * should be placed before or after the second word in the dictionary
 */
public class WordsCompare
{
	//instance fields
	private String wordOne, wordTwo;
	/**
	 *Input for word one and word two
	 *@param one enter word one
	 *@param two enter word two
	 */
	public WordsCompare(String one, String two)
	{
	wordOne=one;
	wordTwo=two;


	}

	/**
	 *compares the two strings
	 *
	 */
	public String getComparison()
	{
		// You must use an if..else statement in this method
		// If you would like to test for the strings being the same, use an if..else..else,
		// but that is optional.  It is not tested in this particular data set.
	
	if(wordOne.compareTo(wordTwo)>0)
	{
		return wordOne+" should be placed after "+wordTwo;
	}
	else if(wordOne.compareTo(wordTwo)<0)
	{
		return wordOne+" should be placed before "+wordTwo;
	}
	else if(wordOne.compareTo(wordTwo)==0)
	{
		return wordOne+" is the same as "+wordTwo;
	}
	
	}[B]//no return statement[/B]
	
	public String getCompareLength()
	{
		// You must use an if..else..else statement in this method	
		
		if (wordOne.length()>wordTwo.length())
		{
			return wordOne+" is longer than "+wordTwo;
		}
		else if (wordOne.length()<wordTwo.length())
		{
			return wordOne+" is shorter than "+wordTwo;
		}
		else if(wordOne.length()==wordTwo.length())
		{
			return wordOne+" is equal to "+wordTwo;
		}
		
	}[B]//No return statement[/B]

}
Member Avatar
ilovejava
Junior Poster in Training
76 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

delete the last else if for each method and put a else
and ure done

Member Avatar
JamesCherrill
... trying to help
10,365 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Your problem is that although you know that one of those if tests must be true, the compiler isn't quite that smart, so it worries about the case where all 3 if tests are false and execution drops out of the last elseif. In that case it gets to the end of the method without returning a String. ilovejava's solution will fix it, or, if you want to keep the very explicit version of your if tests you can just add a final return ""; at the end of each method.

ps @ilovejava - that was a sensible solution, but you will help people even more if you give some explanation of what the problem was caused by, and how your solution fixes it. J.

Member Avatar
hiddepolen
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 1 [?]
 
1
 

I think it would be better to return null instead of "". That way, the rest of the methods can check easier if there was any 'real' return.

Member Avatar
JamesCherrill
... trying to help
10,365 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Yes, in general I agree. In this particular case it's less important because that final return can never be executed.

Member Avatar
hiddepolen
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 1 [?]
 
0
 

Yes, I agree to that too :).

Member Avatar
xiangzhuang
Newbie Poster
6 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks guys :D

Question Answered as of 2 Years Ago by JamesCherrill, hiddepolen and ilovejava
Member Avatar
JamesCherrill
... trying to help
10,365 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

If you're happy with the answers to your question please mark this "solved" so everyone knows there's no more work needed. Thanks.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: