简体   繁体   中英

Netbeans class' method not recognized

I'm having a fairly odd problem with Netbeans.

I'm trying to calculate the sum of an ArrayList, but I am not able to call the method public int sumOfHand() onto my this.hands variable.

I've restarted Netbeans numerous times, created new classes and tried to calculate the sum using the .reduce() method using streams, but none of it helped. Thanks for any suggestion!

import java.util.List;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Collections;

public class Hand implements Comparable<Hand>{
    public ArrayList<Card> hands;
    
    public Hand() {
        this.hands = new ArrayList<>();
    }
    
    public void add(Card card) {
        this.hands.add(card);
    }
    
    public void print() {
        this.hands.forEach(crd -> {System.out.println(crd);});
    }
    
    public void sort() {
        Collections.sort(this.hands, (crd1, crd2) -> crd1.compareTo(crd2));
    }
    
    @Override
    public int compareTo(Hand otherHand) {
       // sumOfHand() not recognized here, 'Cannot find symbol'
       return this.hands.sumOfHand() - otherHand.sumOfHand();
    }
               
    public int sumOfHand() {
        int sum = 0;
        for (Card tc : this.hands) {
            sum += tc.getValue();
        }
        return sum; 
    }
    
}

You can't call a int sumOfHand() of this.hands as hands is an ArrayList.class and int sumOfHand() is a method of Hand.class. To call this method you need to use this.sumOfHands() inside your Hand.class.

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