I'm a beginner and working on a digital attendance system. I'm using the following code in actionPerformed:

for (int i = 1; i < count; i ++) {
            if (ae.getSource () == buttons[i]) {
                switch (available[i]) {
                    case 1: {
                        available[i] = 0;
                        names.remove (unames[i]);
                        System.out.println ("removing " + unames[i]);
                        break;
                    }

                    case 0: {
                        available[i] = 1;
                        names.add (unames[i]);
                        System.out.println ("at A adding" + unames[i]);
                        break;
                    }
                    default: System.out.println ("Default");
                }
                //System.out.println (names);
            }
        }

But, everytime the loop runs when I click a button, both the cases are executing..

are you certain that the actionPerformed is only being called once?

JamesCherrill may be right. To test that idea add a System.out.println("Entered actionPerformed"); before line 1.

However line 5 sets available[i] to 0 then line 11 tests for 0 so maybe it's doing what you coded it to do.
There are many ways to fix that logic. From putting available[i] into a temp variable and performing the switch test on the temp variable to recoding.

line 5 sets available[i] to 0 then line 11 tests for 0

Well, no. Line 5 sets it, line 8 breaks from the switch, and line 11 is never executed. (To be fair, my first guess ws exactly what you said - I had to check the JLS to be sure.)
As far as I can see there's no way for both cases to executed in one pass of that code.

commented: Thanks, I skipped that line and being biologically based made an error. Ooops. +15

Second idea. Line 2. What if that test comes up true when it should not?

More println's to see what it is doing.

Line 2. What if that test comes up true when it should not?

Yes. If two elements of the buttoms array are both equal to the current source then the switch would execute twice, with the observed results.
A real-time trace or some strategic printlns will give the answer.

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.