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.

jungkookie
  • 35
  • 7

2 Answers2

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;
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
  • 1
    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. – Chetan Kinger Apr 23 '15 at 16:05
  • @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 Apr 23 '15 at 16:09
  • @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. – CubeJockey Apr 23 '15 at 16:13
  • @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 Apr 23 '15 at 16:17
  • @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 :) – CubeJockey Apr 23 '15 at 16:24
  • @Trobbins I absolutely need to return strings in my methods, thanks you so much i really appreciate it! – jungkookie Apr 23 '15 at 16:27
  • @jungkookie If my last update doesn't fit what you're trying, you'll probably need to update your original post with your Main method so I can see what you're doing with the Strings. Let me know :) – CubeJockey Apr 23 '15 at 16:31
  • @Trobbins thanks to your edits of the method, my program is now working like it should! I really couldn't thank you enough! :) – jungkookie Apr 23 '15 at 16:34
  • @jungkookie good to hear. If you understand the code changes and why your program works, that will be thanks enough. – CubeJockey Apr 23 '15 at 16:35
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" : "" ;

} 
Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31