Bony Joesadi
Bony Joesadi

Reputation: 27

Show variable in switch statement

switch (name){
    case "Rusia":
       String a="Tuan Rumah"
        break;
    case "Brazil":
        String a="Zona Concacaf"
        break;
}
zona.setText(a);

How to show variable "a" into zona textfield in android studio

Upvotes: 1

Views: 52

Answers (4)

Bita Mirshafiee
Bita Mirshafiee

Reputation: 650

Define variable 'a' outside of switch statement and before it:

String a ="":
Switch()

Upvotes: 0

Michael Dodd
Michael Dodd

Reputation: 10270

You need to define a outside of the switch statement, and then set it inside. For example:

String a = "";
switch (name){
    case "Rusia":
        a = "Tuan Rumah";
        break;
    case "Brazil":
        a = "Zona Concacaf";
        break;
    default:
        a = "Unknown";
}
zona.setText(a);

Upvotes: 3

Khemraj Sharma
Khemraj Sharma

Reputation: 58934

String a = "";
switch (name){
    case "Rusia":
        a = "Tuan Rumah";
        break;
    case "Brazil":
        a = "Zona Concacaf";
        break;
}
if(!TextUtils.isEmpty(a))
    zona.setText(a);

Upvotes: 0

xingbin
xingbin

Reputation: 28269

Define it out the switch block:

    String a = "default";
    switch (name){
        case "Rusia":
            a="Tuan Rumah"
            break;
        case "Brazil":
            a="Zona Concacaf"
            break;
    }
    zona.setText(a);

Upvotes: 1

Related Questions