0

i seem to be having a problem with my code:

 public static String Equilateral (int a, int b, int c){

    if(a==b && b==c)
    return "Equilateral";


} 

It says "missing return statement" when i clearly have a return. Can anybody please help? Thank you very much.

2
  • 1
    You have no return statement for a negative match to the if condition Commented Apr 23, 2015 at 16:07
  • i tried adding "else return null" however, it displays no output when i run the program Commented Apr 23, 2015 at 16:13

2 Answers 2

4

It says that because if your conditional statement evaluates to false then there will be no return statement, your code would just skip over it.

Use this template instead:

if(a==b && b==c){
    return "Equilateral";
}
else{ 
    return "Not Equilateral!";
}

You should use some curly braces, it will help make sense of what you're doing and prevents errors like yours.

Alternatively, if you'd prefer having a single return statement:

String result = "Not Equilateral";

if(a==b && b==c){
    result = "Equilateral";
}
return result;

Edit following OP's clarification:

If you set your code like this, then if your triangle is not equilateral, the returned String will be empty (nothing will be printed with System.out.print()).

String result = "";

if(a==b && b==c){
    result = "Equilateral";
}
return result;
Sign up to request clarification or add additional context in comments.

9 Comments

It would be a good idea to create a String variable in the method, assign it a value in the if or else blocks and return the variable instead of having multiple return statements.
@Trobbins i want my code to skip it, the program should display the nature of the triangle. So if it's not equilateral, it will skip it to the next method and so on. Until the condition is met, then the output would be the nature of the triangle
@jungkookie Am I right in assuming you have multiple methods then, Equilateral(), Isosceles() and Right() ? If that's so then you should make the method return a boolean instead of a String, Or just use a single method which returns the state of your triangle. Let me know which you're trying and I will make an edit to my answer.
@Trobbins Yes i have multiple methods, and i call them in the main method. So that my output would be the nature of the triangle alone. Which means the methods need to return a string, the nature of the triangle. Thank you so much :)
@jungkookie I've updated my answer, let me know if that helps you out at all. If you absolutely need to return strings in your methods, then I will change the answer again :)
|
2

Imagine your if condition returned a false value for (a==b && b==c). What would you return in that case? Still Equilateral?

What you really need to have is

public static String Equilateral (int a, int b, int c){

    if(a==b && b==c)
        return "Equilateral";

    return "";

} 

or in a condensed format

public static String Equilateral (int a, int b, int c){

    return (a==b && b==c)? "Equilateral" : "" ;

} 

6 Comments

I doubt he will understand use of a ternary operator. :)
@Trobbins I just provided alternate solutions. This is the place to learn new and different ways of doing things. ;) Thanks for the comment tho
@Trobbins It's ternary IIRC.
What if in case it's not equilateral, i don't want the program to display nothing. How can i do that? thanks~~
It's all good. We're here to help each other.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.