0
votes

I'm trying to scan a folder of images and add them to an arraylist. I want to combine a scanner and random variable to randomly select one of the images from the array list and attach it to to a private ImageIcon icon. "FemaleFaces" is the directory with the images. So far, here is my code and thanks for any help!

private ImageIcon iconex; 

        File f = new File("FemaleFaces");
        Scan = new Scanner(f);
        ArrayList<ImageIcon> files = new ArrayList<ImageIcon>();
        while(Scan.hasNext())
        {
            files.add(new ImageIcon(Scan.next()));
        }
        Scan.close();

        int Ffindex = new Random().nextInt(files.size());
        iconex = files.get(Ffindex);

Exception in thread "main" java.io.FileNotFoundException: FemaleFaces (Access is denied) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.util.Scanner.(Unknown Source) at Human.(Human.java:66) at testerobjects.main(testerobjects.java:19)

1
Scan.next() will return a string. The string needs to be used to construct an ImageIcon object (using whatever logic you would use) - Patashu

1 Answers

1
votes

Scan.next() returns a String. You need to construct a ImageIcon from the value.

Something along the lines of...

files.add(new ImageIcon(Scan.next()));