Question

So I have this bit of code and I'm having problems with my for loop.

@EventHandler
    public void onPistonExtend(BlockPistonExtendEvent event)
    {
        if (TownStones.blockPistons)
        {
            List<Block> pushedBlocks = event.getBlocks();
            Material theMaterial = null;
            int end = TownStones.blocks;
            boolean done = false;
            if (pushedBlocks != null) {
                for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext(); !done)
                {
                    Block theBlock = (Block)localIterator.next();
                    theMaterial = theBlock.getType();
                    done = false;
                    int i = 0; continue;
                    if (theMaterial == Material.getMaterial(TownStones.blockType[i]))
                    {
                        event.setCancelled(true);
                        done = true;
                    }
                    else if (i > end)
                    {
                        done = true;
                    }
                    i++;
                }
            }
        }
    }

Intellij Idea is telling me !done is not a Statement

Was it helpful?

Solution

That is because !done isn't a statement. The issue is your for loop:

for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext(); !done)

This reads as "to begin, declare localIterator and set it to pushedBlocks.iterator(), then continue while the iterator has more elements". There's no semantic meaning to !done. Rewrite the for loop as:

for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext() && !done;)

OTHER TIPS

A Java for loop must conform to the

for (initialization; termination; increment) {
    statement(s)
}

style. !done is not a valid increment statement. Please refer to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for an introduction on how to use a for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top