Stullif
Stullif

Reputation: 1

Syntax for switch-case in Java

I'm using switch case in Java for the first time and I'm a bit unsure about the syntax. Assuming the setTeamName function works, which it does, would the following function make all of the teams in my array have the placeholder String as it's name or should I from case 0: since i starts at 0?

public static Team[] makeTeams(){
    Team[] teams = new Team[10];
    for(int i = 0; i < teams.length; i++){
        switch(i){
            case 1: teams[0].setTeamName("Arsenal");
            case 2: teams[1].setTeamName("Arsenal");
            case 3: teams[2].setTeamName("Arsenal");
            case 4: teams[3].setTeamName("Arsenal");
            case 5: teams[4].setTeamName("Arsenal");
            case 6: teams[5].setTeamName("Arsenal");
            case 7: teams[6].setTeamName("Arsenal");
            case 8: teams[7].setTeamName("Arsenal");
            case 9: teams[8].setTeamName("Arsenal");
            case 10: teams[9].setTeamName("Arsenal");
        }
    }
    return teams;
}

Upvotes: 0

Views: 1651

Answers (6)

Chris Mantle
Chris Mantle

Reputation: 6693

Your case statements would need to start from 0, because as you rightly observe, i starts at zero. However, this appears to be the least of your problems (unless this is just an exercise in using switch case).

You don't need switch case in this situation at all. Plus, you never create any objects in the array, so every time you attempt to access the array at a particular index, you're going to get a null reference exception. The following will suffice:

Team[] teams = new Team[10];
for (int i = 0; i < teams.length; i++) {
    teams[i] = new Team();
    teams[i].setTeamName("Arsenal");
}

What you've effectively got in your original example is an example of an anti-pattern, the Loop-switch sequence. If you want the original example to work properly using this anti-pattern (for educational purposes only), you need to add break; statements to ensure that your case statements don't fall through:

Team[] teams = new Team[10];
for (int i = 0; i < teams.length; i++) {
    teams[i] = new Team();
    switch (i) {
        case 0: teams[0].setTeamName("Arsenal"); break;
        case 1: teams[1].setTeamName("Arsenal"); break;
        case 2: teams[2].setTeamName("Arsenal"); break;
        case 3: teams[3].setTeamName("Arsenal"); break;
        case 4: teams[4].setTeamName("Arsenal"); break;
        case 5: teams[5].setTeamName("Arsenal"); break;
        case 6: teams[6].setTeamName("Arsenal"); break;
        case 7: teams[7].setTeamName("Arsenal"); break;
        case 8: teams[8].setTeamName("Arsenal"); break;
        case 9: teams[9].setTeamName("Arsenal"); break;
    }
}

Without the breaks, every case statement under the one that matches i is evaluated, e.g. when i == 0, all of the case statements will be executed.

Upvotes: 1

Izold Tytykalo
Izold Tytykalo

Reputation: 719

You have few errors in your code:

1) You do not have case 0 - so it is not used. Suggestion is always use default case.

2) Each case should be finished with break; Otherwise all cases below are executed too. For example for case 9 these cases are called 9 and 10. and for case 1 all 10 cases have been called. (but for case 0 in your code none is called).

3) You had reserved array of 10 teams but you did not populated objects to this array. You code will produce null pointer exception.

Upvotes: 0

almeynman
almeynman

Reputation: 7428

You are doing a lot of unnecessary work. Try

public static Team[] makeTeams(){
    Team[] teams = new Team[10];
        for(int i = 0; i < teams.length; i++){
            teams[i] = new Team();
            teams[i].setTeamName("Arsenal");
        }
    return teams;
}

Upvotes: 0

Alex_ru
Alex_ru

Reputation: 31

Do you really need switch here? for loop will be enough.

Switch just move execution to case row and ignore another case for next time. So when you give i = 1 to switch all of the case statement will be executed. You can prevent it by using break;

switch (i) {
    case 1: 
        teams[0].setTeamName("Arsenal");
        break;
    case 2: 
        teams[1].setTeamName("Arsenal");
        break;
}

Upvotes: 0

HUSNAIN SARWAR
HUSNAIN SARWAR

Reputation: 95

use break Statement after every instruction in case.

Upvotes: 1

Nirmal
Nirmal

Reputation: 1259

You do have options in filling up the array but if your task requires you to use only switch then start with case 0 instead of 1, since 0 is the "first" case you are encountering.

Upvotes: 0

Related Questions