# Thread: Sum the selected index of an array....

1. Member
Join Date
Dec 2011
Posts
1
Rep Power
0

## Sum the selected index of an array....

I am writing a game program for a school assignment and need some help. This game is called 'Take Out'. Basically a user rolls two dice, and selects checkboxes totaling the sum of the roll. If a 10 was rolled, the user could select 8 & 2, 7 & 3, etc. I need to sum the total of the checkboxes selected as error checking - make sure the player isn't cheating. I used an array to create the checkboxes. If I understand correctly, that array only contains the checkboxes - no values, is that correct?

How do I code so if the checkbox is selected, the indexes of the selected checkboxes are added together? Is that even possible?

I also thought maybe I needed another array, consisting of values int 1 - 12 to correspond with the checkbox array. If I do it that way, how do I 'associate' the two arrays so the value is pulled from the second array if the checkbox in the first array is selected?

Code I used to create the checkboxes:

Java Code:
```	//loop to initialize the checkboxes and place them on the panel
for(int i = 1; i < myCheckBox.length; i++){
myCheckBox[i] = new JCheckBox(""+i);```
}

code for second array - if I need to do it that way (?)
Java Code:
```//loop to initialize an array of ints to use for calculation Take Out selected boxes.
for(int i = 1; i < takeOutTotal.length; i++){
takeOutTotal[i] = i;```
}

I did add the following action listener code but read somewhere the checkbox needs a different action?
Java Code:
```	//action
ButtonAction click = new ButtonAction();

for(int i = 1; i < myCheckBox.length; i++){
}```
Basically, the steps of the program are: Dice are rolled; Checkboxes are selected, Checkboxes selected are added together and that sum is compared to the sum of the dice roll; Dice are rolled again - and the selected boxes are disabled.

Here is the complete code for the click action:

Java Code:
```	private class ButtonAction implements ActionListener{
public void actionPerformed( ActionEvent e){
if (e.getSource() == quit){
System.exit(0);
}
Random randomNumbers = new Random();
int dice1 = 0;
int dice2 = 0;

if (e.getSource() == resetButton){
for(int i = 1; i < myCheckBox.length; i++){
myCheckBox[i].setSelected(false);
myCheckBox[i].setEnabled(true);
pass1RB.setEnabled(true);
pass2RB.setEnabled(true);
pass3RB.setEnabled(true);
die1Label.setText("");
die1Label.setIcon(null);
die2Label.setText("");
die2Label.setIcon(null);
instructions.setText("Roll the dice and 'Take Out' the sum of \nyour roll with the numbers left.");
}//end For
}//end reset button

//roll and show die 1
if(e.getSource() == rollDiceButton){
dice1 = 1 + randomNumbers.nextInt( 6 );
dice2 = 1 + randomNumbers.nextInt( 6 );
final int diceSum = dice1 + dice2;  //sum dice

//roll and show die 1
if(dice1 == 1) die1Label.setIcon(die1);
if(dice1 == 2) die1Label.setIcon(die2);
if(dice1 == 3) die1Label.setIcon(die3);
if(dice1 == 4) die1Label.setIcon(die4);
if(dice1 == 5) die1Label.setIcon(die5);
if(dice1 == 6) die1Label.setIcon(die6);
//roll and show die 2
if(dice2 == 1) die2Label.setIcon(die2);
if(dice2 == 2) die2Label.setIcon(die2);
if(dice2 == 3) die2Label.setIcon(die3);
if(dice2 == 4) die2Label.setIcon(die4);
if(dice2 == 5) die2Label.setIcon(die5);
if(dice2 == 6) die2Label.setIcon(die6);

instructions.setText("The sum or your dice is " + diceSum +
"\nWhat numbers would you like to Take Out?" );

//program sums the boxes checked.

int sum = 0;  //sum of enabled checkboxes
for(int i = 1; i < myCheckBox.length; i++){
if (myCheckBox[i].isSelected()){
sum =+ myCheckBox[i]; //this is an error....

instructions.setText("The sum or your choice is " + sum);
//I added the above line to see what the sum was and I get 13
//the code is counting the number of elements, not summing their indexes.
}

for(int d = 1; d < myCheckBox.length; d++){  //go through and disable the checked boxes
if (myCheckBox[d].isSelected()){
myCheckBox[d].setEnabled(false);
}

//code for the user selecting checkboxes that do NOT total the dice roll
//this works! if I use the above code, all rolls according to that should
//be 13 and I do get an error every time.

//  if(sum >= 0 && sum != diceSum){
//			instructions.setText("That entry does NOT match your dice roll! " +
//				"\nPlease press the Restart Button" );
//  for(int r = 1; r < myCheckBox.length; r++){  //go through and disable the checked boxes
//	myCheckBox[r].setEnabled(false);
}//end For
//	}//End Else```

Any assistance would be greatly appreciated!! Beginning programmer Please be patient

2. ## Re: Sum the selected index of an array....

First of all, I would avoid using only one ActionListener and then giving all buttons that same ActionListener, since your buttons do vastly different things. Next, I wouldn't even give the JCheckBoxes ActionListener but instead would have the user select the two JCheckBoxes and then have them press a "Submit" JButton. In that button's ActionListener, I'd loop through the JCheckBox array checking to see which JCheckBox is "selected" and then extracting its text, parsing the text to an int, and summing it. That's it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•