Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Random suit and card with arrays

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Random suit and card with arrays

    Hello!

    I am having some practice with arrays, and I am trying to make a program that picks a random suit and then picks a random card from that suit. Here is the code, I am not finished with it yet:

    public class RandomSuitandCard {
     
    	public static void main(String[] arg) {
     
    		int[] Diamond; 
    		int[] Heart;
    		int[] Club;
    		int[] Spade;
    		int i;  //position in array
    		int i2 = 0;  //number to be out in array
     
    		Diamond = new int[12];
    		Heart = new int[12];
    		Club = new int[12];
    		Spade = new int[12];
     
    		for (i=0, i=12, i++) {
    			i2++;
    			Diamond[i]=i2; }
     
    		for (i=0, i=12, i++) {
    			i2++;
    			Heart[i]=i2; }
     
    		for (i=0, i=12, i++) {
    			i2++;
    			Club[i]=i2; 
    			}	
     
    		for (i=0, i=12, i++) {
    			i2++;
    			Spade[i]=i2; 
    			}	
    		}
    		}

    The for statements are as follows:

    for (i=0, i=12, i++) {
    			i2++;
    			Diamond[i]=i2; }

    But the i++ is underlined in red and it says:

    Syntax error, insert "; ; ) Statement" to complete ForStatement

    This doesn't make any sense! Please help!


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Try putting semicolons (; signs) instead of commas in the for parameters.

  3. The Following User Says Thank You to blackscholes For This Useful Post:

    SilentNite17 (February 29th, 2012)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Random suit and card with arrays

    for (i=0, i=12, i++) {
    for statements don't use commas like that. If you are unsure check out The for Statement in Oracle's Tutorial.

    -----

    It would be a very good idea to follow Java coding conventions and begin variables with a lowercase letter: diamond etc.

  5. #4
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Random suit and card with arrays

    Oh thank you so much!!! It solved it! Thanks again.

  6. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Random suit and card with arrays

    Ok, new problem.

    I made an array array to hold all the suits, so here is the updated code:

    import java.util.Random;  //imports the class needed to pick a random slot from the arrays, I did not want to you use Math.random and make a switch here, that is an alternative
     
    public class RandomSuitandCard {
     
    	public static void main(String[] arg) {
     
    		int[][] suits;
    		int[] diamond; 
    		int[] heart;
    		int[] club;
    		int[] spade;
    		int i;  //position in array
    		int i2 = 0;  //number to be out in array
    		int randomSuit;  //random suit chosen
    		int randomCard;  //random card from random suit chosen
    		String output;
     
    		Random random = new Random();
     
    		suits = new int[3][44];
    		diamond = new int[12];
    		heart = new int[12];
    		club = new int[12];
    		spade = new int[12];
     
    		suits[0] = diamond;
    		suits[1] = heart;
    		suits[3] = club;
    		suits[4] = spade;
     
    		for (i=0; i<=12; i++) {
    			i2++;
    			diamond[i]=i2; }
     
    		for (i=0; i<=12; i++) {
    			i2++;
    			heart[i]=i2; }
     
    		for (i=0; i<=12; i++) {
    			i2++;
    			club[i]=i2; 
    			}	
     
    		for (i=0; i<=12; i++) {
    			i2++;
    			spade[i]=i2; 
    			}	
     
    		randomSuit = (random.nextInt(3));
     
    		switch (randomSuit) {
    		case 0:
    			suits = suits[0];
    		}
    		}
    		}

    So, at the end, at

    randomSuit = (random.nextInt(3));
     
    		switch (randomSuit) {
    		case 0:
    			suits = suits[0];

    I am trying to get the random suit. The suits[0] is underlined in red and it says:

    Type mismatch: cannot convert from int[] to int[][]

    But if you look at the top, it obviously says that it is an array array:

    int[][] suits;


    Help?
    Last edited by SilentNite17; March 1st, 2012 at 12:03 AM. Reason: Misspell

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Random suit and card with arrays

    How would you assign a single array to double array?
    Read the error message carefully and you'll hopefully get to know.

  8. #7
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Random suit and card with arrays

    Ok, here's what I think.

    If your telling me that I need to display it as a suits[][] (array array) than I have already tried that and it does nothing. If you mean that I am trying to assign an array into an array array, then shouldn't that be what it does? I mean, an array is a box that contains numbers. So, an array array is a box of boxes of numbers, or an array that holds arrays, right?

    If you saying that I need to make the one of the suits arrays that are supposed to contain the cards into something different, than please explain.

  9. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Random suit and card with arrays

    What i mean to say is, let's assume you have a two dimensional array,
    int [][] array = new int[5][];
    What does this mean?
    And when you do,
    array = array[0];
    what do you mean by this?

    Tell us, what do you want to do and why do you need to do this?

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Credit card help
    By kostas198 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2011, 10:50 PM
  3. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  4. [SOLVED] BlackJack.. Random Card Assignment?
    By Hallowed in forum Object Oriented Programming
    Replies: 5
    Last Post: April 5th, 2011, 07:25 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM