0

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
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • 4
    Log the exception reason with `e.getMessage()` and `e.printStackTrace()` to see the root cause. – Karol Dowbecki Mar 04 '19 at 11:51
  • 5
    You've only yourself to blame for not knowing what is going on as your exception handling could be much better. At least print out the stack trace within the catch block: `e. printStackTrace();`. No sense flying blind. This way Java will *tell* you what the error is, and where it's happening. – Hovercraft Full Of Eels Mar 04 '19 at 11:51
  • 2
    See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java), you are doing it wrong by using `==` – Karol Dowbecki Mar 04 '19 at 11:52
  • 3
    BTW, have you considered rolling a random `int` instead of double so you don't have to use those intervals? – tobias_k Mar 04 '19 at 11:53
  • 2
    Seriously... catching the error then not printing what it is is like when your doctor asks you to go get an X-ray and you go and you get your X-ray pictures and you rip them up and give the doctor just the radiologist's receipt so he'll know you went. :P – Amadan Mar 04 '19 at 11:56
  • Start with `e.printStackTrace();` – Antoniossss Mar 04 '19 at 11:57
  • You'd better off studying how to use classes and objects. Why don't you create a class `Card` which contains the properties of a card? – MC Emperor Mar 04 '19 at 12:08
  • Besides the `Random.nextInt(int)` method provides a more elegant way to select a random card. In addition, with your current code, it's possible to get three red 9's, while the original game only has two of them in its deck. You should put all possible cards in a `List` and then select a random index. Afterwards, remove the index. – MC Emperor Mar 04 '19 at 12:11
  • Ah I see. Thanks for the tips! All I know is the basics of Java so far. So I really didn't know a lot of what you guys were saying... But thanks for the pointers! – Mavrik Lokeeh Mar 05 '19 at 15:34

4 Answers4

2

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

ldo
  • 27
  • 2
0

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 :)

0

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"))

0

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.

mahieus
  • 580
  • 3
  • 17