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

Était-ce utile?

La 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;)

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top