Problems with if else if statement operation

• May 22nd, 2012, 09:34 PM
Farmer
Problems with if else if statement operation
I am a beginner trying to create a program that selects two random cards from a deck. A pocket pair In doing this I am trying to test the if else if statements in the program. I have used if else if statements to handle the cases where the cards are the same, where the cards are the same twice, where the cards are the same three times and the case where they aren't the same. The if else if statements are either not working correctly or they are working correctly (probably) but I am using them improperly. I set generator.nextInt(2) so that I would have a greater chance at getting the same number to test the if else if statements.
Here is the code:

Code :

```package ppselector;   import java.io.IOException; import java.util.ArrayList; import java.util.Random;   public class PPSelector {   public static void main(String[] args) throws IOException {   String card1 = " "; String card2 = " "; String pp = " "; int cardIndex1 = 0; int cardIndex2 = 0;   ArrayList<String> myPP = new ArrayList<String>(); myPP.add("2c"); myPP.add("3c"); myPP.add("4c"); myPP.add("5c"); myPP.add("6c"); myPP.add("7c"); myPP.add("8c"); myPP.add("9c"); myPP.add("Tc"); myPP.add("Jc"); myPP.add("Qc"); myPP.add("Kc"); myPP.add("Ac");   cardIndex1 = 1; card1 = myPP.get(cardIndex1);   cardIndex2 = 1;   // if else if statements handle the case where card1 == card2 // chances of selecting the same two random numbers between // 1 and 52 three times in a row is slim   if(cardIndex1 != cardIndex2){   card2 = myPP.get(cardIndex2); System.out.println("first");   }else if(cardIndex1 == cardIndex2){   cardIndex2 = generator.nextInt(2); card2 = myPP.get(cardIndex2); System.out.println("2nd"); System.out.println("card1:" + card1 + " cardIndex1:" + cardIndex1); System.out.println("card2:" + card2 + " cardIndex2:" + cardIndex2);   }else if(cardIndex1 == cardIndex2){   cardIndex2 = generator.nextInt(2); card2 = myPP.get(cardIndex2); System.out.println("3rd");   }else { cardIndex2 = generator.nextInt(2); card2 = myPP.get(cardIndex2); System.out.println("last"); //System.out.print(cardIndex2 + ":" + card2 + " "); } System.out.print(card1 +" "+ card2); System.out.println(); } }```

Here are the ouputs:
Code :

```//when cardIndex1 = 0; //and cardIndex2 = 1; //Output: First 2c 3c   //when cardIndex1 = 1; //and cardIndex2 = 1; //Output: 2nd card1:3c cardIndex1:1 card2:2c cardIndex2:0 3c 2c //Even though cardIndex1 = cardIndex2 going into the //if-else-if statements, they are not equal within the statements. Why is this?   //when cardIndex1 = 1; //and cardIndex2 = 1; //Output: 2nd card1:3c cardIndex1:1 card2:3c cardIndex2:1 3c 3c //When cardIndex1 = cardIndex2 within the if else statement // it does not enter the next else if brackets. I want "3rd" or "last" to print. Why doesn't it?```
• May 23rd, 2012, 07:02 AM
KevinWorkman
Re: Problems with if else if statement operation
I'm really not sure what you expect this to do. Your logic doesn't make a ton of sense, given that in an if/else if/else series, only ONE of those blocks will execute. So it makes no sense to have the same condition twice like you do. Only the first one will be executed.
• May 24th, 2012, 12:50 AM
Parranoia
Re: Problems with if else if statement operation
Since you're only comparing those two objects and whether they are equal or not, you only need an if statement followed by an else statement. You have two else if statements that would compute to the same values. The third and fourth statement will never be executed.
• May 25th, 2012, 08:02 AM
Farmer
Re: Problems with if else if statement operation
Quote:

Originally Posted by Parranoia
Since you're only comparing those two objects and whether they are equal or not, you only need an if statement followed by an else statement. You have two else if statements that would compute to the same values. The third and fourth statement will never be executed.

I see now that I can't use the if else statements to do what I want. I want to choose two cards, if card A is equal to card B, pick another card B, if card A is equal to card B, pick another card B, if card A is equal to card B, pick another card B, until card B is not card A. After writing this out, it looks like I can do it with a do while statement. Is this the right path?
• May 29th, 2012, 06:43 AM
KevinWorkman
Re: Problems with if else if statement operation
Quote:

Originally Posted by Farmer
I see now that I can't use the if else statements to do what I want. I want to choose two cards, if card A is equal to card B, pick another card B, if card A is equal to card B, pick another card B, if card A is equal to card B, pick another card B, until card B is not card A. After writing this out, it looks like I can do it with a do while statement. Is this the right path?

What happened when you tried?