2

In my MainActivity I have this lines of code:

    //data.getFirstUserInput()-1 is equal to user input.
     deck.setCardNumberEmpty(data.getFirstUserInput()-1);

       System.out.println("\n Cards on table");
                 ArrayList<Deck> deck= deck.getCards();
                  Collections.sort(cards);
                  for (Deckc: deck
           System.out.print((format("%10s","\033[F[ " + c.getOneSpecificNumber())+ " ]"));}

This prints out my random card numbers. But the problem is they are not numerically sorted,

Any idea what I can do?

Card Class

public class Card {

    private String cardLetter;
    private String cardNumber;
    private String nullValue;

    public Card(String cardLetter, String cardNumber, String nullValue){
        this.cardLetter = cardLetter;
        this.cardNumber = cardNumber;
        this.nullValue = nullValue;
    }

  public String getOneSpecificNumber() { return cardNumber;}
  public void setCardNumber(String x){  cardNumber = x;  }


private ArrayList<Deck> deck = new ArrayList();
public Deck(){
                   char A  = 'A';
    int repeats = 2, numOfCards = 8;
    for ( int i = 0; i<numOfCards;i++){
        for (int j = 0; j<repeats; j++){
            cards.add(new Card((char) ( A + i ) + "",i+1,""));
        }
    }
}

1 Answer 1

5

In order to sort the cards Array as per the cardNumber your Card class has to implement the Comparable and override compareTo().

Here is the code snippet:

public class Card implements Comparable<Card> {

    private String cardLetter;
    private String cardNumber;
    private String nullValue;

    public Card(String cardLetter, String cardNumber, String nullValue){
        this.cardLetter = cardLetter;
        this.cardNumber = cardNumber;
        this.nullValue = nullValue;
    }

    public int compareTo(Card c) {
        if(StringUtils.isEmpty(this.cardNumber) || 
           StringUtils.isEmpty(c.cardNumber)) return -1;
        return Integer.parseInt(this.cardNumber) - Integer.parseInt(c.cardNumber);
    }

    public String getOneSpecificNumber() { return cardNumber; }

    public void setCardNumber(String x) {  cardNumber = x; }
}

StringUtils is part of Apache Commons Lang3, if you do not want to include it in your project then you can do something like this:

if(null == this.cardNumber || "".equals(this.cardNumber) ||
       null == c.cardNumber || "".equals(c.cardNumber)) return -1;

Now you just have to do this in order to sort:

Collections.sort(cards);

Example:

System.out.println("\n Cards on table");
List<Card> cards = deck.getCards();
Collections.sort(cards);
for (Card c: cards) {
    System.out.print((format("%10s","\033[F[ " + c.getOneSpecificNumber())+ " ]"));
}
Sign up to request clarification or add additional context in comments.

12 Comments

public int compareTo(Card c){ return this.cardNumber - c.cardNumber; } I'm not sure what this piece of code does, but It gives me an error. "Bad operand types for binary operator'-' first type: String second type: String
Exception in thread "main" java.lang.NumberFormatException: For input string: "" Any idea why I could get this error? Tried ovveride the annotation but It's the same problem
@user3506 You have null or blank values in cardNumber. Add a check to prevent exception. I have updated the answer using the StringUtils. Decide the return order appropriately in such cases.
I guess your last updated answer is the check right? Or am I suppose to add an nullPointerException in order to prevent following error for the isEmpty method? "Cannot find symbol" "Symbol"method isEmpty(String)" "location class StringUtils"
@user3506 Yes, the updated answer includes the check. If you want to use StringUtils then you'll have to include the Apache Commons Lang3 JAR: mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4 Alternatively you can do something like this: if(null == this.cardNumber || "".equals(this.cardNumber))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.