I'm very new to programming and I have been trying out an exercise. Basically I need to count the number of pairs of cards that I find in an unsorted String of 5 cards. The way I interpreted it was probably wrong, For example, one of the strings looks like this: 'sTh3c9hQ' with s being spades, T being ten, h being hearts ect... I'm not sure why my code isn't working.. Probably for a very logical reasons that I am blind to. Could someone offer me some suggestions? Thanks.
int count = 0;
for(int i = 0; i<hand.length()-2; i+=2){
for(int j = 1; j<hand.length()-3; j+=2){
if(hand.charAt(i) == hand.charAt(i+2) && hand.charAt(j) ==
hand.charAt(j+3)) {
count++;
}
}
}
return count;
The desired output in the case of 'sTh3c9hQ' would be 0, as there are no pairs. The desired output in the case of 'sTh3c9sT' would be 1, as there is one pair. ect. If there were two pairs, count would be 2. If there were three, count would be 3, ect