Results 1 to 5 of 5
- 04-23-2012, 09:08 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
I get an error message;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Card.toString(Deck.java:117)
at Deck.toString(Deck.java:44)
at CardMain.main(Deck.java:147
how could I Add a println() in you toString() method
because I ahave a rank or a suite == 0
so [rank-1] or [suite-1] is -1 ?
Java Code:import java.util.*; public class Deck { private Card[] theCards; private int noOfCards; public Deck() { theCards = new Card[52]; noOfCards = 52; this.fill(); //fill(); } public int getNoOfCards() { return noOfCards; } public Card getCard() { Card a = null; a = theCards[noOfCards-0-9]; noOfCards--; return a; } public String toString() { String deckString = "New deck created\n"; for(int i = 1; i <=5; i++) { deckString += theCards[i].toString() + "\n"; } return deckString; } public void shuffleCards() { Random random = new Random(); Card temp; int pos1, pos2; for(int i = 0; i < 30; i++) { pos1 = random.nextInt(noOfCards); pos2 = random.nextInt(noOfCards); //Swap temp = theCards[pos1]; theCards[pos1] = theCards[pos2]; theCards[pos2] = temp; } } private void fill() { int i, j; int index = 0; for(i = 1; i < 4; i++) { for(j = 1; j < 14; j++) { theCards[index] = new Card(i, j); index++; } } } } class Card { public Card(int rank, int suit) { this.rank = rank; this.rank = suit; } public int getRank() { return rank; } public int getSuit() { return rank; } public String toString() { //System.out.println("rank =" + rank); //System.out.println("rank =" + rank); String info = rankTab[rank-1] + " of " + suitTab[suit-1]; return info; } private int rank, suit; // Tables for converting rand & suit to text private static final String[] rankTab = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; private static final String[] suitTab = { "clubs", "diamonds", "spades", "hearts" }; } class CardMain { public static void main(String[] args) { Deck d = new Deck(); // Initailized with rank 12 (Queen) and suit 3 (spades) Card c = new Card(12, 3); System.out.println("The five cards you have are: " +d.toString()); //d.shuffleCards(); } }
- 04-23-2012, 09:16 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
Check this row:
"this.rank = suit;" in your constructor Card
- 04-23-2012, 09:20 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
what do you mean ?
- 04-23-2012, 09:38 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
It means that the entire thing is more easily solved with enums. Look here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-23-2012, 10:13 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
By junkDNA in forum New To JavaReplies: 3Last Post: 10-20-2011, 04:58 PM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at wee
By Azaz in forum New To JavaReplies: 4Last Post: 02-02-2011, 04:32 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By ladyvie in forum New To JavaReplies: 4Last Post: 10-13-2010, 09:19 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks