Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problems with if else if statement operation

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:
    //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?
    Last edited by Farmer; May 22nd, 2012 at 09:51 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with if else if statement operation

    Quote Originally Posted by Parranoia View Post
    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?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problems with if else if statement operation

    Quote Originally Posted by Farmer View Post
    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Reading input from a CMD operation?
    By Yulfy in forum Java Theory & Questions
    Replies: 1
    Last Post: February 3rd, 2012, 11:41 AM
  2. Java File Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2011, 07:54 AM
  3. Byte Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: March 27th, 2011, 11:19 PM
  4. [SOLVED] If statement problems
    By SnarkKnuckle in forum Loops & Control Statements
    Replies: 2
    Last Post: January 6th, 2011, 11:51 PM
  5. Operation ==
    By meytalg in forum Java Theory & Questions
    Replies: 6
    Last Post: January 4th, 2010, 07:43 PM