-1

This is my first question on StackOverflow, so please don't be to harsh :)

I have an issue with this code. Whenever I try to run it, it keeps giving me "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index x out of bounds for length x at Homework10509.main(Homework10509.java:36)," and I don't know how to fix it. A little help?

x means any number btw

import java.util.*;

public class Homework10509 {

    public static void main(String[] args) {


        Map<String, Integer> map = new HashMap<>();


        map.put("ace", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);
        map.put("five", 5);
        map.put("six", 6);
        map.put("seven", 7);
        map.put("eight", 8);
        map.put("nine", 9);
        map.put("ten", 10);
        map.put("jack", 11);
        map.put("queen", 12);
        map.put("king", 13);
        // 0 1 2 3 4
        String[] cardValue = new String[] { "ace", "two", "three", "four", "five",  };

        Scanner sc = new Scanner(System.in);
        int[] cards = new int[52];

        int n = 52;

        for (int i = 0; i < 52; i++) {
            String s = sc.nextLine();
            cards[i] = map.get(s);
            for (int j = 0; j < 13; j++) {
                if (cardValue[j].equals(s)) {
                    cards[i] = j + 1;
                }
            }
        }

        for (int i = 0; i < 52; i++) {
            System.out.println(cards[i]);
        }

        int pointA = 0;
        int pointB = 0;


        for (int i = 0; i < 52; i++) {

            if (cards[i] == 1) {

                if (i <= 47) {
                    if (!hasHighCard(cards, i, 4)) {
                        if (i % 2 == 0) {
                            System.out.println("Player A scores 4 point(s).");
                            pointA += 4;
                        } else {
                            System.out.println("Player B scores 4 point(s).");
                            pointB += 4;
                        }
                    }
                }

            } else if (cards[i] == 11) {

                if (i <= 50) {

                    if (!hasHighCard(cards, i, 1)) {
                        if (i % 2 == 0) {
                            System.out.println("Player A scores 1 point(s).");
                            pointA += 1;
                        } else {
                            System.out.println("Player B scores 1 point(s).");
                            pointB += 1;
                        }
                    }
                }

            } else if (cards[i] == 12) {

                if (!hasHighCard(cards, i, 2)) {
                    if (i % 2 == 0) {
                        System.out.println("Player A scores 2 point(s).");
                        pointA += 2;
                    } else {
                        System.out.println("Player B scores 2 point(s).");
                        pointB += 2;
                    }
                }

            } else if (cards[i] == 13) {

                if (i <= 48) {

                    if (!hasHighCard(cards, i, 3)) {
                        if (i % 2 == 0) {
                            System.out.println("Player A scores 3 point(s).");
                            pointA += 3;
                        } else {
                            System.out.println("Player B scores 3 point(s).");
                            pointB += 3;
                        }
                    }
                }

            }
        }

        System.out.println("Player A:" + pointA + "point(s).");
        System.out.println("Player B " + pointB + "point(s).");
    }

    public static boolean hasHighCard(int[] cards, int beginIndex, int num) {

        for (int j = 1; j <= num; j++) {
            if (cards[beginIndex + j] == 1 || cards[beginIndex + j] > 10) {
                return true;
            }
        }

        return false;
    }
}

8ullred
  • 1
  • 1
  • your cardValue array is of size 5 but you are iterating j from 0 to 12. Correct it there – Alok Raj May 19 '21 at 00:43
  • In the future, I would suggest that you copy and paste the exact exception. Without this, it takes longer to debug your problem. – NomadMaker May 19 '21 at 01:57

1 Answers1

-2

Also probably you should start the below loop with index j = 0; your last j in the loop is bigger than cards length. Also beginIndex + j must be maximum the cards length -1. Not sure what is 'num' argument.

for (int j = 0; j <= num; j++) {
        if (cards[beginIndex + j] == 1 || cards[beginIndex + j] > 10) {
            return true;
    }
}
Stefan
  • 76
  • 8