简体   繁体   中英

My switch statement is not returning anything?

Im selecting a "Left" from alert dialog and after putting it into convertStatusToCode function should retrun "4" but its not?

final String[] status = {"Left"};

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Pick a Status");
builder.setItems(status, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        BookingStatus = convertStatusToCode(status[which]);

HERE IS MY convertStatusToCode FUNCTION but it is not retruning "4" which it should return.


private String convertStatusToCode(String status) {

    switch (status) {
        case "Processing":
            i = "0";
            break;
        case "Room is Allotted":
            i = "1";
            break;
        case "Sorry All Rooms Are Full":
            i = "2";
            break;
        case "Living":
            i = "3";
            break;
        case "Left":
            i = "4";
            break;
    }
    return i;
}

it should look something like this

  String getStatusCode(String status) {
        switch (status) {
            case "Processing":
                return "0";
            case "Room is Allotted":
                return "1";
            case "Sorry All Rooms Are Full":
                return "2";
            case "Living":
                return "3";
            case "Left":
                return "4";
            default:
                return "default_value";
        }
    }

Based upon what you've added in the comments, your Switch statement (albeit incomplete) shouldn't be the cause of your problem as the other posters are assuming.

You may have a problem with the completness of your switch cases, but that shouldn't stop the switch to work as intended.

You're saying that you DEBUG this and that i has the value of "" which I cannot understand unless you are not having a return value in scope.

The answer posted by Antonis Radz is how I would write that switch if I had to.

If you write your Convert function to return like so, then you don't need the extra i variable, which we don't know where you declared.

If you debug this line of code (like you said you did)

switch(value) and you're telling us that value == "Left" then the switch is receiving the correct value at runtime.

Now your "Case" is executed (when you set a breakpoint in case "Left" . So the switch is, again, performing its function so far.

Then I assume you added another breakpoint in the following line(s):

            i = "4";
            break;

to see what is going on.

The first breakpoint would hit in the assignment i= ... and so the variable i which I assume is declared somewhere in scope as String i (At the very least), is either null or contains an old value. Doesn't matter because the next breakpoint (in the break line) would be reached after i is assigned the value of the String "4" . So if you add a watch to i and inspect it right there, it must have the value of "4".

Then you return this i so, again, it should still be "4".

If you did all this, you would have been able to tell much closer where the variable is not being assigned.

Do like this.

    public String getstatuscode(String status){
        String i="";
        switch (status) {
            case "Processing":
                i = "0";
                break;
            case "Room is Allotted":
                i = "1";
                break;
            case "Sorry All Rooms Are Full":
                i = "2";
                break;
            case "Living":
                i = "3";
                break;
            case "Left":
                i = "4";
                break;
            default:
                i = "-1";

        }
        return i;
    }

Declare i as String or int as your requirement. Method is same.Don't forget to remove quotes if you want to assign i as int.

Another thought to use enum rather than switch-case here which gives more flexibility & reusability.

public enum BookingStatus
{

    PROCESSING("Processing", 1),
    ALLOTED_ROOM("Room is Allotted", 2),
    ALL_FULL("Sorry All Rooms Are Full", 3),
    LIVING("Living", 4),
    LEFT("LEFT", 5);

    private String status;
    private int code;

    BookingStatus(String status, int code)
    {
        this.status = status;
        this.code = code;
    }

    public static BookingStatus getCodeFromStatus(String status)
    {
        for (BookingStatus e : values()) {
            if (e.status.equals(status)) {
                return e;
            }
        }
        return null;
    }

    public static void main(String args[]) {
        System.out.printf("Code for this status is %s", BookingStatus.getCodeFromStatus("Sorry All Rooms Are Full").code);
    }

}
switch (status) {
        case "Processing":
            i = "0";
            break;
        case "Room is Allotted":
            i = "1";
            break;
        case "Sorry All Rooms Are Full":
            i = "2";
            break;
        case "Living":
            i = "3";
            break;
        case "Left":
            i = "4";
          // return "4";
            break;
    }
    return i;
}

use return "4"; you don't print anything in switch cases so switch can't return anything if you want to print in switch cases output then use the print method or return in any programming language.

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