Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Daniel Silva
Daniel Silva
11,209 Points

Unexpected Token of While on For Loop

I'm trying to add images from a folder to an array list however I'm getting an "unexpected token" at the beginning of the for loop, a "cannot resolve symbol 'length' and "identifier expected" on the incrementing of the control variable.

I'm using Intellij as my IDE

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Cards {
    File path = new File("/images");
    List imageCollection = new ArrayList();
    File [] files = path.listFiles();
    for(int i = 0; i < files.length; i++){
        if (files[i].isFile()) {
            imageCollection.add(files[i]);
        }
    }
}

1 Answer

I think you want the code to be within a method. Perhaps a constructor, or the main method, I'm not sure what you are trying to accomplish.

Java also allows you to just place code in a block within a class, but I assume that isn't what you want in this example.