Hi there... I'm pretty new to java, just learning arrays now. I've written a program that deals a "poker hand" to 4 players, using methods, but the problem with that is sometimes the same card gets dealt more than once. I'm now trying to use a boolean array of size 52, with each card initialized to false... and when one gets chosen, it changes to true (so you can't choose a card if it's already true).. And truth be told I really have no clue what to do except

boolean[] deck = new boolean[52];

can anyone give me any insight..? I've no idea where to place it or what else to do. Thanks a bunch (: Sorry if this is a really juvenile question!

Here is my original code:

import javax.swing.JOptionPane;

public class Poker
{
    public static void main (String[]args)
    {
    
    int face = 0, suit = 0;
    int player = 0, round = 0;
    
    System.out.print("Player 1\tPlayer 2\tPlayer 3\tPlayer 4\n");


    for (round = 1; round <= 5; round++)              //round of cards dealt
    {    
        for (player = 1; player <= 4; player++)
        {
            face = 1 + (int) (Math.random() * 13);
            suit = 1 + (int) (Math.random() * 4);

            cardNumber(face);
            cardSuit(suit);
                    
            if (player % 4 == 0)
                System.out.println();
        }
    }
        
    }
        
    
    public static void cardNumber (int face)
    {        
        if (face != 10)
            System.out.print(" ");
            
        switch (face) {
            case 1:        System.out.print("A of ");
                break;
            case 2:        System.out.print("2 of ");
                break;
            case 3:        System.out.print("3 of ");
                break;
            case 4:        System.out.print("4 of ");
                break;
            case 5:        System.out.print("5 of ");
                break;
            case 6:        System.out.print("6 of ");
                break;
            case 7:        System.out.print("7 of ");
                break;
            case 8:        System.out.print("8 of ");
                break;
            case 9:        System.out.print("9 of ");
                break;
            case 10:    System.out.print("10 of ");
                break;
            case 11:    System.out.print("J of ");
                break;
            case 12:    System.out.print("Q of ");
                break;
            case 13:    System.out.print("K of ");
                break;            
        }

    }
    
    public static void cardSuit (int suit)
    {        
        switch (suit) {
            case 1:    System.out.print("Diamonds");
                break;
            case 2:    System.out.print("Hearts");
                break;
            case 3:    System.out.print("Spades");
                break;
            case 4:    System.out.print("Clubs");
                break;
        }
        System.out.print("\t");
    }

    
}

I've changed the program so that it uses arrays now instead of methods... I don't know if it will help more, but here it is (much simpler :D.. also, my first time really being successful with arrays)
I've also made it so instead of saying "Player 1, etc.." you can enter in player names yourself, and it will display those. I was actually suprised I could figure out how to do it!

import javax.swing.JOptionPane;

public class Poker
{
    public static void main (String[]args)
    {

    int player = 0, round = 0;
    int i = 0, j = 0;
    
    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; 
    String[] face = { " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", 
                      " J", " Q", " K", " A" };
    
    for (int names = 0; names < args.length; names++)     //user-entered names
    {
        if (args[names].length() < 8)
            System.out.print(args[names] + "\t\t");
        else if ((args[names].length() >= 8) && (args[names].length() < 16))
            System.out.print(args[names] + "\t");
        else if (args[names].length() >= 16)
            System.out.print("long name!\t");
    }
    System.out.println();
    
    
    for (round = 1; round <= 5; round++)               //round of cards dealt
    {    
        for (player = 1; player <= 4; player++)
        {
            i = (int) (Math.random() * face.length); 
            j = (int) (Math.random() * suit.length); 
            String cardString = (face[i] + " of " + suit[j]); 

            System.out.print(cardString + "\t");
            if (player % 4 == 0)
                System.out.println();
        }
    }

    }    
}

Still haven't figured out the boolean array thing though :/

And truth be told I really have no clue what to do except

boolean[] deck = new boolean[52];

can anyone give me any insight..? I've no idea where to place it or what else to do.

Guess you wanna know how to use an array. Here is a crash course:
1. Declare and define an array:

type var_name[] = new type[array_size] ;

Where type could be a basic type (like int, boolean...) or a user defined type (like Poker)
2. Access a member of array which is located at index/position N:
Use the subscript operator (i.e. "[]" ) like this var_name[N].

System.out.println( "Nth element = " + String.valueOf(var_name[N]) ) ;
/*PS: valueOf() is overloaded for basic types only, won't work for user-defined types*/

To assign a value:

var_name[N] = new type() ;

3. Loop thru an array: array_variable_name.length gives you the length of an array.

for( int i = 0; i < var_name.length; i++ )
     System.out.println( String.valueOf( var_name[i] ) ) ;

Here is a full example:

public class test {
    public static void main( String[] args ) {
        //declare and define
        boolean arr[] = new boolean[10] ;
        //init
        for( int i = 0; i < arr.length; i++ )
            arr[i] = false ;

        //access an element
        arr[(int) (Math.random() * 10)] = true ;

        //print the contents
        for( int i = 0; i < arr.length; i++ )
            System.out.println( String.valueOf( arr[i] ) ) ;
    }
}

Oh! Thank you (:

So I think I've figured it out now... by inserting

boolean [] deck = new boolean[52];
    for(int k = 0; k < 52; k++)
        deck[k] = false;

at the beginning

and changing my nested for loop to include:

if (deck[j*13 + i] == false)
            {
                deck[j*13 + i] = true;

                System.out.print(cardString + "\t");
                if (player % 4 == 0)
                    System.out.println();
            }
            else
                player--;

It appears to work now :D

interesting
I am suggesting an entirely different approach to the card generation.
use two int values one for the card color and one for the card itself
card color cant be more than 4
card number cant be more than 13
Then use the random number generator of the Math class to produce those values.
If you dont want the same cards dealt within the same game tick off the values by creating Map and setting the dealt value accordingly.

Should work

interesting
I am suggesting an entirely different approach to the card generation.
use two int values one for the card color and one for the card itself
card color cant be more than 4
card number cant be more than 13
Then use the random number generator of the Math class to produce those values.
If you dont want the same cards dealt within the same game tick off the values by creating Map and setting the dealt value accordingly.

And why d'u think this is an easier way than the one he's already using?!

Should work

That shouldn't be the aim. :)

This question has already been answered. Start a new discussion instead.