0

I am trying to write a code in which I construct a 52 card pile, then deal the cards out to n number of players (it is possible for some players to have an extra card). The winner is the one with the Ace of Spades card.

Here is my program:

public class CardGame {
  public static void main(String[] args) { 
    System.out.println("Enter the number of players");

    int numofPlayers = Integer.parseInt(args[0]);
    CardPile gameDeck = CardPile.makeFullDeck(); 
    CardPile [] players = new CardPile[numofPlayers];

    for (int i=0;i<numofPlayers;i++) {
      int numofnum = i%numofPlayers;
      players[i] = new CardPile();
    }

    for (int i=0;i<52;i++) {
      int numofnum =i%numofPlayers;
      CardPile curPlayer = players[i%numofPlayers];
      Card nextCard = gameDeck.get(i);
      players[numofnum].addToBottom(nextCard); 

    }
    for (int i=1;i<numofPlayers;i++) {
      if (players[i].find(Suit.SPADES, Value.ACE) != -1) {
        System.out.println("Player" + i + "has won!");
      }
    }

  }
}

I keep getting an out of bounds error. The methods that I am calling in this program are well written so the problem should come from this code. Can anyone help ?

Edit : Here is the error that I get

java.lang.ArrayIndexOutOfBoundsException: 0
    at CardGame.main(CardGame.java:5)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
> 

Thanks !

Jayzpeer
  • 81
  • 3
  • 9
  • 1
    exactly in which line do you get the `ArrayOutOfBoundsException`? please also post the full stacktrace of the error. – Blip Nov 21 '15 at 17:05
  • could you point out the line no 5 of `CardGame` class? – Blip Nov 21 '15 at 17:18
  • It's the command line argument right ? – Jayzpeer Nov 21 '15 at 17:22
  • @Jayzpeer how do you run your java class after it is compiled? To be able to use args[0] you need to pass arguments when running you java class. For example > **java CardGame 5** here 5 will be stored in args[0]. I think your code is run like > *java CardGame* and no argument is passed hence args[0] does not exist. You can either supply argument or use *Scanner* to take input from user. – Raf Nov 21 '15 at 17:33

2 Answers2

3

You're asking for the number of players, but not reading input; instead you're reading the args to the program to figure out the number of players.

Likely you're not passing any arguments on the command line, so it's throwing an exception when you ask for args[0].

You'll want to look into getting input from console within the program, or pass the number of players when you run the program (in which case the println can be removed).

Alex Feinman
  • 5,393
  • 1
  • 30
  • 48
3

As Alex explained in his answer, the reason is because you are not passing arguments when running your code. If you wish for the code to work then you have to run your code as follow:

java CardGame 5 

The above executes your CardGame class and pass 5 as argument to the main method in args[0]. If you are executing the code via some IDE let's assume Eclipse then please see the answer in this question to figure out how to pass argument.

If you wish to replace your code above (to accept input from user) then please replace the following line

int numofPlayers = Integer.parseInt(args[0]);

With the following line

Scanner input= new Scanner(System.in);
int numofPlayers = input.nextInt(); 

After executing the code will ask you to input the number of players and input a +ve integer value.

If you use the Scanner option then make sure you validate your input against values that are not integer (as well as negative integer). For example, if the input is provided as anything but, integer then you will get InputMismatchException hence, surrounding your input with try{} and catch(){} to catch above exception would be the right way to go about it.

Community
  • 1
  • 1
Raf
  • 7,505
  • 1
  • 42
  • 59