简体   繁体   中英

My program caught an error I don't know how to fix

So I'm trying to code UNO in Java, and I'm still trying to generate cards. I'm not too sure what the problem here is, but for some reason, my code catches an error inside my methods. I've checked my code a few times already and it isn't a syntax error, so I legitimately don't know what's going on with it.

I've temporarily stopped coding this for now so that I won't create any more errors before you guys tell me what's wrong, to make it easier to modify it. Please tell me what I did wrong!

public class JavaUNO {
    public static void main(String[] args) throws Exception {
        boolean inProgress = false;
        boolean drawCard = false;
        String[][] playerDeck = {{}};
        byte playerDeckLength = 0;

        // MAIN OUTPUT
        try {
            // INITIALIZATION
            Scanner scan = new Scanner(System.in);

            // PROGRAM STARTING PROMPT
            System.out.println("> Deck:");

            // **PLAYER DECK INIT**
            try {
                System.out.println("> Cards Generated:");
                while (playerDeckLength < 7) {
                    // **CARD GENERATION**
                    try {
                        // INITIALIZATION
                        double randType = Math.random();
                        double randColor = Math.random();
                        playerDeck[playerDeckLength][0] = "";
                        playerDeck[playerDeckLength][1] = "";

                        // GENERATES RANDOM CARD TYPE
                        if (randType < 0.066) {
                            playerDeck[playerDeckLength][0] = "0";
                        } else if (randType < 0.132) {
                            playerDeck[playerDeckLength][0] = "1";
                        } else if (randType < 0.198) {
                            playerDeck[playerDeckLength][0] = "2";
                        } else if (randType < 0.264) {
                            playerDeck[playerDeckLength][0] = "3";
                        } else if (randType < 0.33) {
                            playerDeck[playerDeckLength][0] = "4";
                        } else if (randType < 0.396) {
                            playerDeck[playerDeckLength][0] = "5";
                        } else if (randType < 0.462) {
                            playerDeck[playerDeckLength][0] = "6";
                        } else if (randType < 0.528) {
                            playerDeck[playerDeckLength][0] = "7";
                        } else if (randType < 0.594) {
                            playerDeck[playerDeckLength][0] = "8";
                        } else if (randType < 0.66) {
                            playerDeck[playerDeckLength][0] = "9";
                        } else if (randType < 0.726) {
                            playerDeck[playerDeckLength][0] = "Reverse Cycle";
                        } else if (randType < 0.792) {
                            playerDeck[playerDeckLength][0] = "+2 Cards";
                        } else if (randType < 0.858) {
                            playerDeck[playerDeckLength][0] = "+4 Cards";
                        } else if (randType < 0.924) {
                            playerDeck[playerDeckLength][0] = "Skip Turn";
                        } else if (randType < 1) {
                            playerDeck[playerDeckLength][0] = "Color Change";
                        }

                        //GENERATES RANDOM CARD COLOR
                        if (randColor < 0.25) {
                            playerDeck[playerDeckLength][1] = "Blue";
                        } else if (randColor < 0.5) {
                            playerDeck[playerDeckLength][1] = "Yellow";
                        } else if (randColor < 0.75) {
                            playerDeck[playerDeckLength][1] = "Red";
                        } else if (randColor < 1) {
                            playerDeck[playerDeckLength][1] = "Green";
                        }

                        //CHECKS IF CARD IS WILDCARD
                        if (playerDeck[playerDeckLength][0] == "+4 Cards") {
                            playerDeck[playerDeckLength][1] = "Wildcard";
                        } else if (playerDeck[playerDeckLength][0] == "+2 Cards") {
                            playerDeck[playerDeckLength][1] = "Wildcard";
                        } else if (playerDeck[playerDeckLength][0] == "Color Change") {
                            playerDeck[playerDeckLength][1] = "Wildcard";
                        }

                        playerDeckLength += 1;
                    } catch (Exception e) {
                        System.out.println("");
                        System.out.println("> An uncaught error occured!");
                        System.out.println("> Location: Card Generation");
                    }
                    System.out.println("Type: " + playerDeck[playerDeckLength][0] + "; Color: " + 

playerDeck[playerDeckLength][1]);
                }
            } catch (Exception e) {
                System.out.println("");
                System.out.println("> An uncaught error occured!");
                System.out.println("> Location: Player Deck Init");
            }
        } catch (Exception e) {
            System.out.println("");
            System.out.println("> An uncaught error occured!");
            System.out.println("> Location: Main Output");
        }
    }
}

COMMAND PROMPT:

> Deck:
> Cards Generated:

> An uncaught error occurred!
> Location: Card Generation

> An uncaught error occurred!
> Location: Player Deck Init

You are initializing an empty two dimensional string array. The code tries to access an index that is not allocated so i think the program is probably throwing IndexOutOfBounds exception

Seems to be a lot of code but very few class / functions ;)

First of all try to organize a bit better your code, it will be easier to debug, modify and maintain it ... I also invite you to read about Exception and Exception handling in Java, you will see that using Exception every time lead to many problems !

Of course, some of us will be able to make your program work, but honestly, you just need to read a bit more and you'll make it :)

You don't init your array and you probably taking "IndexOutOfBounds" error. Try init your array with something like this: "String[][] playerDeck = new String[7][2];". Also you need to change your checks from playerDeck[playerDeckLength][0] == "+4 Cards" to if (playerDeck[playerDeckLength][0].equalsIgnoreCase("+4 Cards"))

The problem is that you initialise an empty (2 dimensional) array. When you try to access this it will give an index out of bound exception. If you know the size you'll have to initiate it with that size.

Other that that, please check the comments on your question. This should help you to solve these problems yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM