0

I had just started learning programming and facing problem like in this one, can anyone help me what i am doing wrong here and how can i resolve it?

IntelliJ saying to add return statement but I had already added it!

public class Main {

    public static void main(String[] args) {
        double calcscore = calcFeetAndInchesToCentimeters(7, 7);
    }

    public static double calcFeetAndInchesToCentimeters(double feet, double inches) {
        if ((feet >= 0) || (inches >= 0 && inches <= 12)) {
            double centimeters = (feet * 12) * 2.54;
            centimeters += inches * 2.54;
            System.out.println(feet + " feet, " + inches + " inches = " + centimeters + " cm ");
            return centimeters;
        } else if ((feet < 0) || (inches < 0 && inches > 12)) {
            return -1;
        }
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 5
    You need to add it for all possible paths of your code. You have an `if` and an `else if` with return but what if neither of the conditions are true? So add a return at the end of the method – Joakim Danielson Jan 04 '21 at 17:45

1 Answers1

1

Refer this for the explanation

"Missing return statement" within if / for / while

Missing return statement for if/else statement

Below code will work for the time being

 public class Main {

public static void main(String[] args) {
    double calcscore = calcFeetAndInchesToCentimeters(7, 7);
    System.out.println(calcscore);
}

public static double calcFeetAndInchesToCentimeters(double feet, double inches) {
    double centimeters = 0;
    if ((feet >= 0) || (inches >= 0 && inches <= 12)) {
        centimeters = (feet * 12) * 2.54;
        centimeters += inches * 2.54;
        return centimeters;
    } else if ((feet < 0) || (inches < 0 && inches > 12)) {
        return -1;
    }
    return centimeters;

}}

or

public class Main {

public static void main(String[] args) {
    double calcscore = calcFeetAndInchesToCentimeters(7, 7);
    System.out.println(calcscore);
}

public static double calcFeetAndInchesToCentimeters(double feet, double inches) {
    double centimeters = 0;
    if ((feet >= 0) || (inches >= 0 && inches <= 12)) {
        centimeters = (feet * 12) * 2.54;
        centimeters += inches * 2.54;
        return centimeters;
    } else if ((feet < 0) || (inches < 0 && inches > 12)) {
        return -1;
    } else {
        return centimeters;
    }

}}
Sibin Rasiya
  • 1,132
  • 1
  • 11
  • 15