Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2006, 06:35 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Card shuffling problem!

I am a beginning java student and I am having trouble with a class that I am supposed to create which creates a deck of cards(without suit) and I am suppose to create a an algorithm that simulates shuffling the deck.

I think the logic is right but I keep getting these two errors. Can some one here help me with this?


deck.java:16: illegal start of type
for (int i=0; i < numOfCardsInSuit; i++)
^
deck.java:33: <identifier> expected
^
2 errors

//******************************Out line of deck Class***********************************


public class Deck{


private int[] cards = new int[52];
private int numOfRandShuffles = 10000;// # Card Shuffles
private int numOfCardsInSuit = 13;

private Random randGen = new Random(); // Random number generator

//initialize the array to the ints 1-13 (4 of each value)
for (int i=0; i < numOfCardsInSuit; i++)
{
      cards[i] = i+1;
      cards[i+1] = i+1;
      cards[i+2] = i+1;
      cards[i+3] = i+1;

}

//shuffle by exchanging each element randomly a specific number of times( in this case 10000)
for (int i=0; i < numOfRandShuffles; i++)
{
    int randomPosition = randGen.nextInt(cards.length);
    int temp = cards[i];
    cards[i] = cards[randomPosition];
    cards[randomPosition] = temp;
}

}//end of Deck class


 
Old January 30th, 2006, 06:04 AM
Authorized User
 
Join Date: Jan 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava
Default

In Java language, you cannot put the code in the declaration section. This will cause error at compiler time.
What you need to do is to put your intialization code in your constructor since your constructor will get call when an object is instantiated.

// Here is a example of how you can initialize your deck of card:
public class Deck
{

   public Deck()
   {
       initializeCardDeck();
   }

   private void initializeCardDeck()
   {
      // put your initialization code in this method
   }
}

 
Old January 30th, 2006, 12:57 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank u alot.

It works.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Greeting Card in C# ? KellyRainmaster BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 October 30th, 2007 05:06 PM
ASP Shopping card problem - Urgent please hugoscp Classic ASP Professional 1 June 12th, 2007 06:42 PM
Wild Card Help * Corey Access 1 March 1st, 2007 11:42 AM
Flash Card Karen_ajkk Java Basics 0 February 12th, 2007 03:05 AM
problem whit shoping card mbcreator PHP How-To 0 February 17th, 2006 05:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.