1

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]);
        }
    }
}
User1990
  • 85
  • 2
  • 14

2 Answers2

2

You forgot to declare a method

public class Cards {

    void readCollection() { // <-- Here!
        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]);
            }
        }
    } // Don't forget the closing bracket
}
Bill
  • 400
  • 2
  • 6
0

You only have a class, you need to place the code within a Java function. Something like this:

public class Cards {
    public static void main(String[] args) {
        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]);
            }
        }
    }
}
duper51
  • 770
  • 1
  • 5
  • 20