简体   繁体   中英

Creating a Helper method in Java

So Im creating a Helper method in Java to calculate postage based on size, but I can't seem to figure out the return part. Im still new to helper methods and accessors etc. Im using Eclipse and its telling me "Add return statement" but I did.. What am I doing wrong here?

Here is my code:

   //Helper Method.
   public int calculatePostageCost() {
    double postCost;

    if(satchelSize.equals("small")) 
        postCost = 10;

    else if(satchelSize.equals("Medium") || satchelSize.equals("medium"))
        postCost = 13;

    else if(satchelSize.equalsIgnoreCase("Large") || satchelSize.equals("large"))
        postCost = 17;

    else {
        return calculatePostageCost();

    }
}

The problem is that your return statement is within the scope of the else statement ,it should be outside like this : `

public int calculatePostageCost() {
 double postCost;

 if(satchelSize.equals("small")) 
    postCost = 10;


else if (satchelSize.equals("Medium") || satchelSize.equals("medium")){
    postCost = 13; 


else if(satchelSize.equalsIgnoreCase("Large") || satchelSize.equals("large"))
    postCost = 17;

    return postCost;
}

If you return calculatePostageCost() you create a recursive loop which causes a stack overflow error.

Do like this,

//Helper Method.
   public int calculatePostageCost() {
    int postCost = 5; // i don't know about default conndition, i am taking 5

    if(satchelSize.equals("small")) 
        postCost = 10;

    else if(satchelSize.equals("Medium") || satchelSize.equals("medium"))
        postCost = 13;

    else if(satchelSize.equalsIgnoreCase("Large") || satchelSize.equals("large"))
        postCost = 17;    
    }
    return postCost ;
}

Don't use strings to compare your size, create an enum to do that:

public enum Size {
   SMALL, MEDIUM, LARGE
}

private Size satchelSize;  ....
public int calculatePostageCost() {
   switch(satchelSize) {
      case SMALL:
         return 10;

      case MEDIUM:
         return 13;

     case LARGE:
         return 17;
   }
}

If you are very keen on keeping the strings you can switch on strings too:

private String stachelSize = ....;

public int calculatePostageCost() {
   switch(satchelSize.toUpperCase()) {
      case "SMALL":
         return 10;

      case "MEDIUM":
         return 13;

     case "LARGE":
         return 17;

     default:
         throw new AssertionError("Don't know satchel size " + satchelSize);
   }
}

Note that your original code had

 else {
    return calculatePostageCost();

}

Which would call the same function again, which would end up in the same else branch, calling the same function again, which would end up in the same else branch, which.... would eventually give a StackOverflowException .

(I understand that strictly it does not answer your question 'why doesn't this compile'.)

The problem is that you do not have a guaranteed return statement at the end of the function. What would happen if your function does not encounter a satchel size, which is either "small", "medium", etc, you will return the value of what your function calculatePostageCost returns (I will return to that later).

In every other case, however, you do not have a return in your function. When you encounter "small" as satchel size, you enter the first if block of code, where you will set postCost to 10, then you jump over the rest of the code (since it is all else if).

Most likely you are missing a statement like return postCode; below the else block. This would at least eliminate the error message from eclipse. I am not fully sure about your code, but you could have an endless recursion here. Your else block might be a problem:

else {
    return calculatePostageCost();
}

You need to check if it is possible, that in the next call of this recursion, the else block will not be reached. When this is not the case, you will have an endless recursion everytime you enter this function while you are in a state where the satchel size is not "small", "medium", etc, because you won't have a chance to change the state and get out of these calls anymore.

You have to return value on every possible scenario. Right now you are returning (infinite recursion thus stak overflow will happen) only single in case if package is not small not medium nor large. You have to return value for every of this variants, like that:

   public int calculatePostageCost() {
    int postCost=1234; // default cost for not small nor medium nor large package

        if(satchelSize.equals("small")) 
            postCost = 10;

        else if(satchelSize.equals("Medium") || satchelSize.equals("medium"))
            postCost = 13;

        else if(satchelSize.equalsIgnoreCase("Large") || satchelSize.equals("large"))
            postCost = 17;
    return postCode

}

Or even better

       public int calculatePostageCost() {
            if(satchelSize.equalsIgnoreCase("small")) 
                return 10;

            else if(satchelSize.equalsIgnoreCase("Medium"))
                return 13

            else if(satchelSize.equalsIgnoreCase("Large"))
                return 17;

        return 12345; // cos of non small, medium nor large package
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM