-1

The program is designed to scan the user for the first two cards they receive and tell them what they have. Unfortunately, when it asks "Are the cards suited?: " it terminates before input can be taken. I also could learn how to organize the conditions in a switch statement rather than all these ugly if-else statements as well.

package Test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {

            // TODO Auto-generated method stub
            int card1 = 0;
            int card2 = 0;
            String fc1, fc2;
            String answer;


            Scanner scan = new Scanner (System.in);
            System.out.println("First card: ");
            if (scan.hasNextInt())
            {
                    card1 = scan.nextInt();
            }
            else
            {
                    fc1 = scan.next();

                    if (fc1 == "A")
                    {
                            card1 = 14;
                    }
                    else if(fc1 == "K")
                    {
                            card1 = 13;
                    }
                    else if (fc1 == "Q")
                    {
                            card1 = 12;
                    }
                    else if (fc1 == "J")
                    {
                            card1 = 11;
                    }
            }

            System.out.println("Second card: ");
            if (scan.hasNextInt())
            {
                    card2 = scan.nextInt();
            }
            else
            {
                    fc2 = scan.next();
                    if (fc2 == "A")
                    {
                            card2 = 14;
                    }
                    else if (fc2 == "K")
                    {
                            card2 = 13;
                    }
                    else if (fc2 == "Q")
                    {
                            card2 = 12;
                    }
                    else if (fc2 == "J")
                    {
                            card2 = 11;
                    }
            }

         if (card1 == card2)

         {
             System.out.println("You have a pair.");
         }
         else 
         {
        System.out.println("Are your cards suited?(y/n): ");
        answer = scan.nextLine();

             if (answer == "y")
             {
                 if (card1 >12 && card2 > 12)
                 {
                     System.out.println("High value suited cards.");
                 }
                 else
                     if (card1 == card2++ || card1++ == card2)
                     {
                         System.out.println("Suited connectors.");
                     }
             }
             else
                 if(answer == "N")
                 {
                     if (card1 >=12 && card2 >= 12)
                     {
                         System.out.println("High value cards, hold.");
                     }
                     else
                         if(card1 > 5 && card2 > 5)
                         {
                             if(card1++ == card2 || card1 == card2++)
                             {
                                 System.out.println("Connectors.");
                             }
                             else
                             {
                                 System.out.println("Fold");
                             }
                        if (answer != "y" || answer != "n")
                        {
                                System.out.println("Invalid entry.");
                        }
                     }
             }



         }
             }                

}

MGL94
  • 15
  • 2
  • 5
    using equals() instead of == for String/Object comparison. – kosa Dec 02 '13 at 21:42
  • 4
    Welcome to SO. Please read the [FAQ] and [Ask] for tips on writing good SO questions. Have you tried running this in an IDE debugger and walking through the code one step at a time to see what's happening? That should be your first endeavor. In general, posting a bunch of code with no evidence you've made an effort to debug it yourself (i.e. tell us what you've done so far) is considered bad form. Also, you _"could learn how to organize the conditions in a switch statement"_. I'm sure you could, and that would involve just a few minutes reading about `switch` statements. – Jim Garrison Dec 02 '13 at 21:45
  • but what if the answer is either `Y` or `n`? – Sam I am says Reinstate Monica Dec 02 '13 at 21:47
  • 1
    @Nambari so, how many times now do you think this question has been asked on stackoverflow? – Cruncher Dec 02 '13 at 21:51
  • 1
    On top of everything said in this question, for god's sake, use an enum!!!!!!!!!! – Cruncher Dec 02 '13 at 21:52
  • @Cruncher Yes, enums would work. However, if the asker is asking a question like this, and doesn't know how to use a switch statement... they won't know what an enum is, none the less how to make one xD – Sir_Mr_Bman Dec 03 '13 at 00:36

1 Answers1

1

The program terminates because it takes the newline character the end-user added after the last integer as the end of the nextLine().

Let's say the user enters 123 and presses Enter. At this point, the data in the buffer looks like this:

[1] [2] [3] [\n]

After you call scan.nextInt() the integer part is removed, but the [\n] remains in the buffer. When you call scan.nextLine(), that end-of-line marker is the first character the scanner sees, so it treats that \n as an empty string, which is returned to your program. You can fix it by calling scan.nextLine() before taking a full-line input.

However, this by itself will not fix your program, because you should be comparing strings using equals(), e.g. like this

if (fc1.equals("A"))

not like this

if (fc1 == "A") // <<== This will not work correctly
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523