简体   繁体   中英

Enhanced For-Loop Gets Stuck

I have two files PokerCards and Frame.

PokerCards is enumeration *my suits work fine*
enum Number {
        Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King;

        public static final Number[] numbers = Number.values();
        public static Number getNumber(int n){
            return Number.numbers[n];
        }
    }

    private final Number number;

    //constructor
    public PokerCards(final Suit suit, final Number number){
        this.suit = suit;
        this.number = number;
    }


    public Number getNumber(){
        return this.number;
    }

Below is Frame which is to be my GUI. My error checking for suits work perfectly but my numbers...not so much. I am trying to use an enhanced for-loop to match the corresponding enumeration

//card1Num is the value of the card such as Ace, Three, King and is recorded via a TextField via the GUI.

card1Num = card1TextFieldNum.getText();
if (card1Suit.equalsIgnoreCase("Diamonds")){
                    for (PokerCards.Number n : PokerCards.Number.values()) {     //GETS STUCK
                        if(n.name().equals(card1Num)) {
                            card1 = new PokerCards(PokerCards.Suit.Diamonds, PokerCards.Number.valueOf(card1Num));
                            System.out.println("Card 1 good to GO");
                            card1Good = true;
                        } else {
                            JFrame popUpError2 = new JFrame();
                            JOptionPane.showMessageDialog(popUpError2, "Incorrect Input, Number Input for First Card is incorrect! Please double check" +
                                    " your input", "ERROR", JOptionPane.ERROR_MESSAGE);
                            break;
                        }
                    }

Everything works lovely except the enhanced for-loop. As i test other cards i get prompted by my error. It only accepts 'Ace', the first element. Why is my enhanced for-loop getting stuck?

The problem is that you should fail only if no cards match, not if only the first card doesn't, so you want to move the failing code to outside the loop like so:

for (PokerCards.Number n : PokerCards.Number.values()) {
    if(n.name().equals(card1Num)) {
        card1 = new PokerCards(PokerCards.Suit.Diamonds, PokerCards.Number.valueOf(card1Num));
        System.out.println("Card 1 good to GO");
        card1Good = true;
    }
}
if (!card1Good)
{
    JFrame popUpError2 = new JFrame();
    JOptionPane.showMessageDialog(popUpError2, "Incorrect Input, Number Input for First Card is incorrect! Please double check" +
                                    " your input", "ERROR", JOptionPane.ERROR_MESSAGE);
}

The for loop works fine, the if/else logic is the issue behind errors being thrown on cards other then 'Ace'.

As you mentioned yourself - if you use 'Ace' your code will work without error because 'Ace' is the first element in Number.values() and the check n.name().equals(card1Num) will be true , triggering the code in your if statement. All other cards will fail on this and throw an error because they will be first matched against 'Ace'. If I understand correctly what you want to do I think you should use findAny() instead of your for loop. The code would be something like this:

PokerCards.Number.values().stream().filter(number -> 
    number.name().equals(card1Num)).findAny().ifPresentOrElse(card ->  
        {your_if_logic_here}, 
        {your_else_logic_here})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM