How to stop null from printing

1.1k views Asked by At

When executing my program it keeps displaying "null" on line 13 I want to know what's wrong on my algorithm for it keeps printing null.

private class SpadeIterator implements Iterator<Card>{
    private int nextCardSpade;
    private List<Card> cards;
    private int count=0;
    private SpadeIterator(List cards) {
        this.cards=cards;
        this.nextCardSpade = cards.size()-1;
    }

    @Override
    public boolean hasNext() {
        count++;
        if(nextCardSpade<0)
            return false;
        //nextCardSpade--;
        return true;
    }

   @Override
    public Card next() {

        int i=0;
        this.count=i;
        Card temp = cards.get(nextCardSpade);

        while(hasNext()){    //find SPADES
            temp=cards.get(nextCardSpade--);
            i++;

            if(temp.suit.value == Suit.SPADES.value)
                return temp;
        }
        //DONT MOVE
        return null;
        //nextCardSpade--;      //DONT DELETE

    }
}

Current Results

The results are meant to show the 13 spades without returning null at the end.

2

There are 2 answers

0
Ori Marko On

Check that nextCardSpade equals to 0 also:

if (nextCardSpade <= 0)
2
Pshemo On

Your next() method shouldn't contain any case which would return invalid value like null. If there is no next element to be returned it is hasNext() method's job to return false to prevent you from calling next().

So your code should look more like

class SpadeIterator implements Iterator<Card>{
    private int spadesCounter = 0;
    private Iterator<Card> cardsIt;

    private SpadeIterator(List<Card> cards) {
        cardsIt = cards.iterator();
    }

    @Override
    public boolean hasNext() {
        return spadesCounter<13; // we can't put spacesCounter++ here because 
                                 // we should be able to call `hasNext()` many times
                                 // and still get same answer,
                                 // so `hasNext()` shouldn't change any state 
                                 // (at least one which could cause changing its result)
    }

    @Override
    public Card next() {
        Card temp = cardsIt.next(); //if our `hasNext()` returned `false` but we 
                                    //didn't check or ignored it, this will CORRECTLY 
                                    //throw NoSuchElementException 
        while(temp.suit.value != Suit.SPADES.value){
            temp = cardsIt.next();
        }
        spadesCounter++;
        return temp;
    }
}

OR if you simply want to iterate over list and print only selected elements you can use streams with filtering like

List<Card> cards = ...//not really important how get it
cards.stream()
     .filter(card -> card.suit.value == Suit.SPADES.value)
     .forEach(card -> System.out.println(card));

or even simpler

for (Card card : cards){
    if(card.suit.value == Suit.SPADES.value){
        System.out.println(card);
    }
}