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!

Recommended Answers

All 2 Replies

break?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.