1

Replacing userinput with the card index

presented on a new row, and is not replacing the actual number the user typed. I've tried to put the letter and the number in the same arraylist but I have no idea how to display these.

Community
  • 1
  • 1
  • You are calling deck,shufflecard which is shuffling your list. After that you are calling getcards(i) --> which is user input. as per the logic the card out put is correct. – user3509208 Mar 03 '16 at 20:02

2 Answers2

1

one thing I see you should replace String secondCardLetter = card.getCardLetter(); with String secondCardLetter = deck.getCards(i); otherwise second card is same as first try that and let me know

Also recommend replacing ArrayList<Card> card = new ArrayList<Card>(); with List<Card> cards = new ArrayList<Card>(); please note the variable name change too since its a collection of Card(s). Initialize the deck with a loop

    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,""));
        }
    }

Please pay attention to your names as the to depict what the method may be doing. You getCards method takes an integer and returns the "Card", where as getCards shouldnt take any parameters and simply return the cards list you have stored. If you want to return a specific card, declare a method called public Card getCard(int i) notice the missing letter s as it shows this will return one card, and on the other hand public List<Card> getCards() method would return a List of cards, then that for loop would work.

Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
  • what do you mean randomize the index number? I would shuffle the deck in the Deck Constructor – Zaid Qureshi Mar 03 '16 at 20:13
  • sorry I forgot to add the char cast, check now, you would replace in the deck constructor where you are adding firstCard secondCard etc replace that entire thing with that and it will add it, and I would still perfer the list being cards over card, hope that helps – Zaid Qureshi Mar 03 '16 at 20:39
  • Glad it worked, when you increase the points, replace the letter, add a method to Card setCardLetter where you replace the Card letter with empty string, then add a method in deck setEmpty(index i) which would set card letter of the `cards.get(i).setCardLetter("");`. So when you are increasing your points do `deck.setCardEmpty(data.getFirstUserInput());` then do it for second, let me know if you understand this. If every thing works, please accept the answer thank you – Zaid Qureshi Mar 03 '16 at 21:02
  • first thing is that your set method should be void unless you are using the return values. and replace this in Card class `public void setCardLetter(String letter){ cardLetter = letter; }` if that doesnt do it update the code you have right in the question – Zaid Qureshi Mar 03 '16 at 22:24
  • its okay dont worry I understand let me know if it works or not – Zaid Qureshi Mar 03 '16 at 22:28
  • You can make sure by printing the cards before and after setting null – Zaid Qureshi Mar 03 '16 at 22:44
  • not sure how that loop works out but I would print it like `for (Card c: deck.getCards()){System.out.println(c.getLetter());)` – Zaid Qureshi Mar 04 '16 at 00:07
  • `deck.getCards()` would be a method that you would implement in Deck which would return the cards list – Zaid Qureshi Mar 04 '16 at 17:59
  • glad it helped. Again, even the it may not seem important, naming convention matters a lot. Give names that make sense. Even though it may be tempting to not care about them as the code will work eventually with or without proper names. It improves maintainability and readability. – Zaid Qureshi Mar 06 '16 at 21:58
0

If I understand you correctly, what you are looking to do is to keep replacing the characters in the terminal output, without breaking the output into new lines right? i.e: a counter going from 1 to 100?

For that you need to use the carriage return character: '\r'

I think this is the line you are printing the letter of the card right?

System.out.println(format("%10s","\n[ " + firstCardLetter + " ]"));

Try changing this to

System.out.println(format("%10s","\r[ " + firstCardLetter + " ]"));

edit Sorry what you have to use is the \033[F character, as mentioned here

System.out.println(format("%10s","\033[F[ " + firstCardLetter + " ]"));
Community
  • 1
  • 1
Pubudu
  • 964
  • 1
  • 7
  • 17