This code works fine so far... except for my "isPair()" function. It gives me the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string:

Any help solving this issue would be great. I am trying to get the program to read poker hands and print out what it is. If I can get the first one the rest should be easy. Thanks for any help.

package poker;

import java.util.*;

public class Poker
{
    private static String[] suites = {"H","C","D","S"};
    private static String[] cards = new String[52];
    private static String[][] hands = new String[1][5];

    public boolean isStraight(int[] rank)
    {
        int count = 0;

        for(int i = 0; i < 13; i++)
        {
            if(rank[i] != 0)
            {
                count++;
                if(i == 13 && rank[1] != 0)
                    count++;
            }
            else
                count = 0;
            if(count == 5)
                return true;
        }
        return false;
    }

    public static void main(String[] args)
    {
        loadCardArray();
        shuffleCards();
        dealCards();
        showCards();
        isPair();
        playAgain();
    }

    private static void loadCardArray()
    {
        int i = 0;
        for(int s = 0; s < 4; s++)
        {
            for(int j = 1; j < 14; j++)
            {
                switch(j)
                {
                    case 10:
                        cards[i] = 'T' + suites[s]; break;
                    case 11:
                        cards[i] = 'J' + suites[s]; break;
                    case 12:
                        cards[i] = 'Q' + suites[s]; break;
                    case 13:
                        cards[i] = 'K' + suites[s]; break;
                    case 1:
                        cards[i] = 'A' + suites[s]; break;
                    default:
                        cards[i] = j + suites[s]; break;
                }
                i++;
            }
        }
    }

    private static void shuffleCards()
    {
        for(int i = 0; i < 100; i++)
        {
            String savedCard = "";
            int variant = ((int)(Math.random() * 50)) + 1;
            for(int j = 0; j < cards.length; j++)
            {
                if(j + variant < cards.length)
                {
                    savedCard = cards[j];
                    cards[j] = cards[j + variant];
                    cards[j + variant] = savedCard;
                }
            }
        }
    }

    private static void dealCards()
    {
        int i = 0;
        for(int k = 0; k < 5; k++)
        {
            for(int j = 0; j < 1; j++)
            {
                hands[j][k] = cards[i];
                i++;
            }
        }
    }

    private static void showCards()
    {
        for(int i = 0; i < hands.length; i++)
        {
            for(int j = 0; j < hands[i].length; j++)
            {
                System.out.print(hands[i][j] + " ");
            }
            System.out.println();
        }
    }

    ///////////////////////
    ///////////////////////
    private static boolean isPair()
    {
        for(int i = 0; i < cards.length; ++i)
        {
            if(Integer.parseInt(cards[i]) == 2)
            {
                System.out.println("This is a Pair");
                return true;
            }
        }
        return false;
    }
    /////////////////
    /////////////////

    private static void playAgain()
    {
        String play;
        Scanner userInput = new Scanner(System.in);
        System.out.print("Play again? ");
        play = userInput.next();

        if(play.equalsIgnoreCase("y"))
        {
            loadCardArray();
            shuffleCards();
            dealCards();
            showCards();
            isPair();
            playAgain();
        }
        else if(play.equalsIgnoreCase("n"))
        {
            System.exit(0);
        }
        else
        {
            playAgain();
        }
    }
}

Recommended Answers

All 4 Replies

The array cards has chars in it
You can't convert "JS" to a number.

Maybe you need the OFFSET into the chars array.
or
Maybe you want to count if a particular card appears in the list in the hand exactly twice.

How would I do that? I am new to Java and arrays aren't really my greatest skill.

OK. So you just edited your post. Here is what I have:

Entire program works with exception of the "isPair()" function.
Program throws an:
Exception in thread "main" java.lang.NumberFormatException: For input string:

What function "isPair()" is supposed to do:
Examine the cards in hand to see if there are 2 cards that have the same number. If so, then it prints out "Pair". Otherwise nothing.

It would look something like this:
Sometimes I complicate things too much, but I'm thinking you'd need a hasPair(), then maybe a getPair() so you could see which ones were pairs.

class IsPair
{
	public static boolean hasPair(String[] hand)
	{
		boolean blnRetVal = false;
		int[] arr_intCounts = new int[hand.length];

		String strTarget = "";

		for(int intLoop=0; intLoop < hand.length; intLoop++)
		{
			strTarget = hand[intLoop].substring(0,1);

			for(int intInnerLoop=0; intInnerLoop < hand.length; intInnerLoop++)
			{
				if(hand[intInnerLoop].substring(0,1).equals(strTarget))
				{
					arr_intCounts[intLoop]++;
				}
			}
		}

		for(int intLoop=0; intLoop < hand.length; intLoop++)
		{
			if(2 == arr_intCounts[intLoop])
			{
				blnRetVal = true;
				break;
			}
		}

		return blnRetVal;
	}

        //tester
	public static void main(String[] args)
	{
		String[] hand1 = {"AS", "JS", "2S", "3D", "9D"};
		String[] hand2 = {"AD", "JD", "JC", "3S", "9H"};
		String[] hand3 = {"AC", "AD", "AH", "3H", "9C"};

		System.out.println(hasPair(hand1));
		System.out.println(hasPair(hand2));
		System.out.println(hasPair(hand3));
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.