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();
}
}
}