Results 1 to 5 of 5
  1. #1
    stephanie904 is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

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

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    Check this row:
    "this.rank = suit;" in your constructor Card

  3. #3
    stephanie904 is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    what do you mean ?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    Quote Originally Posted by stephanie904 View Post
    what do you mean ?
    It means that the entire thing is more easily solved with enums. Look here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    Quote Originally Posted by stephanie904 View Post
    what do you mean ?
    I mean check if it is correct... is it?

Similar Threads

  1. Replies: 3
    Last Post: 10-20-2011, 05:58 PM
  2. Replies: 4
    Last Post: 02-02-2011, 05:32 AM
  3. Replies: 4
    Last Post: 10-13-2010, 10:19 AM
  4. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •