Hans van der Laan
Hans van der Laan

Reputation: 545

Strange Bug with User Input and Switch Statement

public class VoteTUIView {

    VoteMachine voteMachine;

    public VoteTUIView(VoteMachine voteMachine) {
        this.voteMachine = voteMachine;
        this.start();

    }

    public static String errorMissingVariable = "This command needs an variable";
    public static String errorPartyNoneExistant = "This party does not exist";
    public static String errorAddImpossible = "You can't add something to that";
    public static String help = "Seriously? You need help? You look like a smart boy/girl, you can figure this out yourself. I believe in you! ";


    public void start(){
        System.out.println("Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP) ");
        Scanner inputScanner = new Scanner(System.in);
        while(inputScanner.hasNextLine()){
            String inputCommand = inputScanner.next();
            switch (inputCommand){
                case "VOTE": {
                    if(inputScanner.hasNext()){
                        String inputCommand2 = inputScanner.next();
                        if(voteMachine.getParties().getParties().contains(inputCommand2)){
                            voteMachine.vote(inputCommand2);
                        }
                        else{
                            System.out.println(errorPartyNoneExistant);
                        }
                    }
                    else{
                        System.out.println(errorMissingVariable);
                    }
                }
                case "ADD": {
                    if(inputScanner.next().equals("PARTY")) {
                        if(inputScanner.hasNext()) {
                            voteMachine.addParty(inputScanner.next());
                        }
                        else{
                            System.out.println(errorMissingVariable);
                        }
                    }
                    else {
                        showError(errorAddImpossible);
                    }
                }
                case "VOTES": {
                    showVotes(voteMachine.getVotes().getVoteList());
                }
                case "PARTIES": {
                    showParties(voteMachine.getParties().getParties());
                }
                case "EXIT": {
                    break;
                }
                case "HELP": {
                    System.out.println(help);
                }
                default: {
                }
            }

            System.out.println("\nPlease insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP) ");
        }
    }

    public void showVotes(Map<String, Integer> voteList) {
        int i = 1;
        for(String partyName : voteList.keySet()){
            System.out.println(i+": "+partyName+" has obtained "+voteList.get(partyName)+" votes.");
            i++;
        }
    }

    private void showParties(List<String> partyList) {
        int i = 1;
        for(String partyName : partyList){
            System.out.println(i+": "+partyName);
            i++;
        }
    }

    private void showError(String error) {
        System.out.println("Error: " + error);
    }        
}

I am fighting with a strange bug. The program reads the user input and determines which action to take with a switch but often, multiple cases in the switch are triggered when they clearly shouldn't be. It's driving me mad. Does anybody know why it does that?

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
ADD PARTY DemoCant's  
1: DemoCant's

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
ADD PARTY RepublicRats  
1: DemoCant's  
2: RepublicRats

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
VOTE DemoCant's  
VOTE DemoCant's 
Error: You can't add something to that  
1: DemoCant's has obtained 1 votes.  
1: DemoCant's  
2: RepublicRats

Upvotes: 2

Views: 92

Answers (5)

Peter Lawrey
Peter Lawrey

Reputation: 533670

I am fighting with a strange bug.

Which is why you would have used your debugger before asking a question.

The program reads the user input and determines which action to take with a switch but often, multiple cases in the switch are triggered when they clearly shouldn't be.

since there is no break; or return; there is no reason to break out of the switch.

Note: Java, C, C++, C# all do this.

It's driving me mad. Does anybody know why it does that?

Because that is what it is supposed to do.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159844

You're missing break statements on almost all switch cases so the code just falls though to the next case. See the section on fall though in the docs

case "VOTE": {
    ....
    break;
} 
.....

Upvotes: 3

JavaMan
JavaMan

Reputation: 351

You are missing break statement. Without a break statement it is an expected behavior.
You can do more reading about this here http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Upvotes: 1

Abhishek
Abhishek

Reputation: 878

add the break statement after each Case

If you don't add a break statement the code flow will go into the next case until all the case ends or till a break is encountered

Upvotes: 0

Mureinik
Mureinik

Reputation: 311843

The cases in a switch statement fall through, and continue executing the next case. If you don't want your case to fall through, add a break; statement and the end of the case. E.g.:

switch (inputCommand){
    case "VOTE":
        // code for voting
        break;
    case "ADD": {
        // code for adding
        break;
    // etc...

Upvotes: 3

Related Questions