hey there,

I have the following code:

String[][] candidates = new String[4][3];
         for(int i = 0; i!=4;  i++){
            for(int j = 0; j!=3; j++){
               switch (j){
                  case 0:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give name of candidate " + i);}
                  case 1:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give address of candidate " + i);}
                  case 2:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give age of candidate" + i);}

I expect it to build a two dimensional array of type string. The aim is to simulate a database that holds the name, address, and age of each of four candidates competeing in an election.

I thought the switch statement would work properly, but what I get is that the application first asks me for a candidates name, then for his address, then for his age, again for his address, again for his age, and again for his age, and only after this does it prompt me for the second' candidates info. and when it begins asking me for the second's candidate info, it again asks me twice for his/her address and three times for his/her age.

numerically, I expect to be prompted 12 times, but what I get is I'm being prompted 24 times.

I tried to build the same using an if statement for each case and it worked perfectly well.

I would greatly apprectiate it if someone told me what's going on? Why isn't switch statement working as I expected it?

thanks in advance!

6 Years
Discussion Span
Last Post by bibiki

this is emberrasing... break happened to be what was missing. thanks a whole lot.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.