I am trying to get my private void transfer method and pass it into a 2D array transfer2D method, after which I am able to print it out using the print2D_1 method. I am also trying to sort the array from the highest suit to the lowest suit followed by the highest rank to the lowest rank e.g. S12, S9, H13, D09, C10. Please advice.

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

enum SuitEnum
{
    Spade ('S'),
    Heart ('H'),
    Diamond ('D'),
    Club ('C');

public char suit;

SuitEnum(char suit)
{
    this.suit = suit;
}
// Accessor getter
public char getSuit ()
{
    return suit;
}
}

enum RankEnum
{
    Two ('2'),
    Three ('3'),
    Four ('4'),
    Five ('5'),
    Six ('6'),
    Seven ('7'),
    Eight ('8'),
    Nine ('9'),
    Ten ('T'),
    Jack ('J'),
    Queen ('Q'),
    King ('K'), 
    Ace ('A');

    public char rank;

    RankEnum(char rank)
    {
        this.rank = rank;
    }
// Acessor getter
    public char getRank ()
    {
        return rank;
    }
}

class PlayingCard
{
    private SuitEnum suit;
    private RankEnum rank;
    private PlayingCard pc;

    //constructor
    public PlayingCard(SuitEnum suit, RankEnum rank)
    {
    this.suit = suit;
    this.rank = rank;
}

//copy constructor
public PlayingCard(PlayingCard pc)
{
    this.pc = pc;
}

//accessor get method
public SuitEnum getSuit()
{
    return suit;
}

public RankEnum getRank()
{
    return rank;
}

//setter
public void setCard(SuitEnum suit, RankEnum rank)
{
    this.suit = suit;
    this.rank = rank;
}

@Override
public String toString()
{
    return String.format("%3s%s",suit.getSuit(),rank.getRank());
}
}

class ChuaWeiheng_A1 
{
private final int MAXC = 13; 
private final int MAXD = 52;

private void deckOfCards(ArrayList<PlayingCard> values)
{
    for (SuitEnum suit : SuitEnum.values())
    {
        for (RankEnum rank: RankEnum.values())
            values.add(new PlayingCard (suit, rank));
    }
}

private void printDeck(ArrayList<PlayingCard>values)
{
    int count = 0;

    System.out.println("Printing from ArrayList");
    System.out.println();
    {
        for (PlayingCard s : values)
        {
            count++;
            System.out.print(s); 
            if (count == MAXC)
            {
            System.out.println();
            count = 0;  
            }             
        }
    }
    System.out.println("-------------------------");

}

private void listToArray(ArrayList<PlayingCard> values, PlayingCard[] valuesArray)
{
    int i = 0;
    for (PlayingCard s: values)
    {
        valuesArray [i] = s;
        ++i;
    }
}

private void printDeck(PlayingCard[] valuesArray)
{
    int count = 0;
    System.out.println("Printing from Array");
    System.out.println();  
    for (PlayingCard s : valuesArray)
    {
        count++;
        System.out.print(s); 
        if (count == MAXC)
        {
        System.out.println();
        count = 0;
        }             
    }
    System.out.println("-------------------------"); 
}

private void transfer(PlayingCard[] cardArray, String[] strArray)
{
    String[] arrayNo = new String[]{"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14"};
    String[] arrayLetter = new String[]{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    int i = 0;
    for (PlayingCard s : cardArray)
    {
        String currentCard = s.getRank().toString();

        for (int j = 0; j < 13; j++)
        {
            if (currentCard.equals(arrayLetter[j]))
            {
                strArray[i] = s.getSuit().toString().charAt(0) + arrayNo[j];
            }
        }   
            i++;
    }
}

private void printStringArray(String[] strArray)
{
    int count = 0;
    System.out.println("Printing from string array");
    System.out.println();
    for (int i = 0; i < MAXD; i++)
    {
        count++;
        System.out.print("  " + strArray[i]); 
        if (count == MAXC)
        {
            System.out.println();
            count = 0;
        }            
    }     
    System.out.println("-------------------------");
}

// shuffle 
private void shuffle(PlayingCard[] valuesArray)
{
    Random random = ThreadLocalRandom.current();
    int count = 0;
    for (int k = valuesArray.length -1; k> 0; k --)
    {
        int i = random.nextInt(k+1);
        PlayingCard j = valuesArray[i];
        valuesArray[i] = valuesArray[k];
        valuesArray[k] = j;
    }
    System.out.println("Shuffle the cards - Array Version");
    System.out.println("Printing from array");
    System.out.println();
    {
        for (PlayingCard s : valuesArray)
        {
            count++;
            System.out.print(s); 
            if (count == MAXC)
            {
            System.out.println();
            count = 0;
            }             
        }           
    System.out.println("-------------------------");        
    }
}

private void transfer2D(String[][] twoD, String[] strArray)
{
    int rows = 4;
    int columns = 13;
twoD = new String[rows][columns];

for(int i = 0; i < twoD.rows; i++)
    {   
        for(int j = 0; j < twoD.columns; j++)
        {
            twoD[i][j] = strArray[(i*twoD.columns)+j];
        }
    }
}

private void print2D_1(String[][] twoD)
{
    int count = 0;
    System.out.println("Printing from string array");
    System.out.println();

    for(int[] a : twoD)
    {
        for(int i : a)
        {
        }

    }
}

public static void main(String[] args) 
{
    ArrayList<PlayingCard>X = new ArrayList<PlayingCard> ();   
    ChuaWeiheng_A1 T1 = new ChuaWeiheng_A1();
    PlayingCard [] T2 = new PlayingCard[T1.MAXD];
    String[] strArr = new String[52];

    T1.deckOfCards(X);
    T1.printDeck(X);
    T1.listToArray(X,T2);
    T1.printDeck(T2);
    T1.shuffle(T2);
    T1.transfer(T2, strArr);
    T1.printStringArray(strArr);
}   
}

I don't know Java myself so I can't really be of much assistance. However, I'd like to steer you in a better direction so you can hopefully get some good responses. I see you really just posted 300 lines of code here without showing what part of it works and where it's broken.

It's a bit overwhelming to ask someone to read all 300 lines of code. Perhaps specify the specific lines they should focus on and what's currently happening that shouldn't be, if you're getting an error message, etc.

I would suggest you explain a little more about what you're specifically trying to do. Frankly, there's quite a bit in your code that's hard find reasons for. I strongly suspect that you may need a major refactoring. Never fear though, we can help you.

Hi
It looks like you are going about the transfer/print thing in a way that's making your life harder. In best OO style you should get rid of those two methods and put function where it belongs...
You seem to want to show the rank as "Two" ... "Ace", so just use the names that you have already created for the enum menbers (via its name() or toString() methods).
You seem to want to display each card as a String with its suit and rank, so that's something that should be in PlayingCard as its toString() method.
Now you can scrap the transfer method and the print is just a trivial loop to println each card.

To sort the array you need a Comparator that compares the suit and rank of two cards. Then use Arrays.sort(array, comparator). You'll find lots of examples on the web.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.