Sunday, 8 September 2013

Exception in thread "main" java.lang.StringIndexOutOfBoundsException

Exception in thread "main" java.lang.StringIndexOutOfBoundsException

Here's what I've got:
if(readHand.length() == 39) {
for(int i = 0; i <= 36; i+=3) {
nextCard = readHand.substring(i, i+2);
System.out.println(nextCard);
if ((!validRank.contains(nextCard.charAt(0)))
||(!validSuit.contains(nextCard.charAt(1)))) {
return errorMessage = "Error: There are incorrect card
types in the hand.";
}
}
I don't know if I'm just blatantly overlooking something, but when i
reaches 36, the substring should be reading from 36 to 38 which then sets
nextCard as the substring of readHand at 36,37. i then gets incremented to
39 which is greater than 36 and should break out of the for-loop. So why
then am I getting:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 40
at java.lang.String.substring(String.java:1955)
at lab1.evaluateHand(lab1.java:103)
at lab1.readHand(lab1.java:63)
at lab1.main(lab1.java:51)
Thanks for any suggestions!
EDIT:
This is the output displaying the 13 cards in the readHand string and the
message from previously, that's all it gives me =/
2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 40
at java.lang.String.substring(String.java:1955)
at lab1.evaluateHand(lab1.java:103)
at lab1.readHand(lab1.java:63)
at lab1.main(lab1.java:51)

No comments:

Post a Comment