1 Replies - 715 Views - Last Post: 23 April 2012 - 12:00 PM Rate Topic: -----

#1 stephanie904  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-February 12

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExce

Posted 23 April 2012 - 11:54 AM

why do i get ?

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)


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-1];
					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();
					 
		 }
		 
}


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExce

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExce

Posted 23 April 2012 - 11:59 AM

Add a println() in you toString() method
obviously you ahave a rank or a suite == 0
so [rank-1] or [suite-1] is -1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1