0

I am extremely new to Java programming and I am trying to make a Poker Hand Evaluator. I am asking the suit and value of a card, using a for loop. For some reason, it works the first iteration of the for loop but only asks me for one value after that. Here is my code:

import java.util.Scanner;
public class PokerRun {

public static void main(String[] args) {

    int [] suit = new int[5];
    int [] value = new int[20];
    Card card1 = new Card();
    Scanner in = new Scanner(System.in);
    int counter = 1;
    System.out.println("Welcome to the Poker Hand Evaluator!");
    for(int i = 1; i<6; i++)
    {
        System.out.println("What is the suit of card " + i + "?\nPlease type the suit in all lowercase letters: ");
        card1.suit = in.nextLine();
        System.out.println("What is the value of card " + i + "?  (J = 11, Q = 12 K = 13, A = 14");
        card1.value = in.nextInt();
        //checks if face card, if true, then changes card.facecard
        if(card1.value == 11)
            card1.facecard = "Jack";
        else if(card1.value == 12)
            card1.facecard = "Queen";
        else if(card1.value == 13)
            card1.facecard = "King";
        else if(card1.value == 12)
            card1.facecard = "Ace";

        if(card1.value<11)
            System.out.println("You entered a " + card1.value + " of " + card1.suit + ".");
        else
            System.out.println("You entered a " + card1.facecard + " of " + card1.suit + ".");
    }

}

}
masonc15
  • 912
  • 2
  • 10
  • 16
  • Show the command line input and output. – eebbesen Jul 25 '13 at 14:25
  • Please don't depend on user to give expected input. Make your program ready for wierd input. Like user can pass `"abc"`, where you are reading an `int`. Pre-check for input first. – Rohit Jain Jul 25 '13 at 14:28

2 Answers2

2

Rohit Jain, yes it is exactly that problem.

my suggestion for a solution is to exchange the line

card1.value = in.nextInt();

with:

card1.value= Integer.parseInt(in.nextLine());

This should work for you.

To explain it a bit more Scanner was build for String parsing of files etc. And it is not so good for commandline inputs. I would use a BufferedReader:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
Sorontur
  • 470
  • 4
  • 15
1

When you call a next method from the Scanner class, if there is no input available, it freezes the program until you press the enter key (and input a new line character.) When it unfreezes the program however, if the call to the next method doesn't use all the input, it will continue to run through any future next commands until it runs out. Only then will it again wait for the enter command.

In this case, the input to your nextInt command is overlapping with the next iteration's nextLine command.

To show you what I mean by that (since those are just words) ->

Let's say you input

Diamond, and then press the enter key.

This will input into the program

'Diamond\n'

The first call to in.nextLine(); will absorb every character up until the new line, so card1.suit = "Diamond".

Next let's say you input the number 3, and then press the enter key.

This will input

'3\n'

The call to in.nextInt(); will absorb every character up until the end of the number. However it will NOT absorbe the new line, so

card1.value will be equal to 3.

However it will still have to do something with the rest of the input '\n'

So when it reaches the next iteration of the loop, the second call to in.nextLine() will absorb every character up until the '\n', which is to say, the empty space.

so now card1.value = ""

To fix this, you can add a call to in.nextLine() right after the call to in.nextInt(), or you can do Integer.parseInt(in.nextLine()).

dcco
  • 75
  • 8