Having a little bit of trouble with the iterator next(). Can't seem to get it to work properly. I've been working on this code for a while so I was thinking another set of eyes would help.
This is my deck class with creates a list of Card objects, I'm trying to make a method to grab the next Card in the list starting with the first one:
package blackjack;
import blackjack.Card.Rank;
import blackjack.Card.Suit;
import java.util.*;
public class Deck {
public ArrayList<Card> cards = new ArrayList<>();
int i;
Card next;
public Deck() {
initializeDeck();
}
public void printDeck() {
for (Card c: cards)
System.out.println(c);
}
private void initializeDeck() {
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards.add(new Card(rank, suit));
}
}
}
public Card getNextCard() {
if (cards.listIterator().hasNext() != true) {
getNextCard();
}
else {
next = cards.listIterator().next();
}
return next;
}
}
This is my main class where I call the getNextCard() and what I'm thinking it should do is print the first and then the next Card in the list but what it's doing is printing the first Card twice.
package blackjack;
import java.util.*;
public class BlackJack {
public static void main(String[] args) {
Deck deck = new Deck();
System.out.println(deck.getNextCard());
System.out.println(deck.getNextCard());
}
}
Thanks in advance for any help!
getNextCard()!