roketto 0 Newbie Poster

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

roketto 0 Newbie Poster

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 :/

roketto 0 Newbie Poster

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 …