#modulo java error

18 messages · Page 1 of 1 (latest)

undone light
#

Can someone help me fix the code

#
public final class ModuloFilterPipe extends Pipe{
    private final int divisor;
    private Integer nextInt;

    public ModuloFilterPipe(Pipe feedingPipe, int divisor){
        super(feedingPipe);
        this.divisor = divisor;
        this.nextInt = null;
    }
    public boolean hasNextInteger() {      
        while(this.getFeedingPipe().hasNextInteger()) {/* delegate the logic to the feeding pipe, because this one would not know */
            this.nextInt = this.getFeedingPipe().nextInteger();
            if((this.nextInt % this.divisor) == 0) {
                return true;
            }
        }
        return false;
    }
    public Integer nextInteger() {  
        Integer result = null;
        if(this.nextInt != null) { 
            result = this.nextInt;                                      /* pull the value from the feeding pipe, and apply the operation */
        }else {
            while(this.getFeedingPipe().hasNextInteger()) {
                nextInt = this.getFeedingPipe().nextInteger();
                if((nextInt % this.divisor) == 0) {
                    result = nextInt;
                }
            }
        }
        return result;
    }
}
halcyon kiln
#

hasNextInteger shouldn't modify the state ig

undone light
#

i have to gitve it back in hasNextInteger

halcyon kiln
#

I meant hasNextInteger should probably not have side affects/modify anything

undone light
#

okey i changed the return value

#

but now the one above is wrong

halcyon kiln
#

if the hasNext method returns true, the next call afterwards needs to return something non-null

#

and if hasNext returns false, the next call needs to return null

#

I think the problem is that hasNextInteger method consumes the data and then nextInteger has different data

undone light
#

yeah its just one part of a big project i did

#

the are 5 calsses total, but with this modulo im still fighting

#

i changed it, but its still not going

#

😭