I am a java beginner... Now I am writing a program which, when you input a number k, it shows the name of the kth image in the folder "Image". But now when I input any number, the result would always be null.
I need some help;(
Here is my code:
public class Main {
//create array, total length is number of images in file "Image"
static int length = (new File("/Users/Sam/Desktop/Image").listFiles().length)-1;
static String[] myArray = new String[length];
public static void listFilesForFolder(final File folder) {
//put every file name into the array
for (final File fileEntry : folder.listFiles()) {
int i = 0;
myArray[i] = fileEntry.getName();
i++;
}
}
public static void main(String[] args){
final File folder = new File("/Users/Sam/Desktop/Image");
listFilesForFolder(folder);
//input a number x
Scanner k = new Scanner(System.in);
System.out.println("Choose a frame: ");
int a=k.nextInt();
//show the xth image's name
System.out.println(myArray[a]);
k.close();
}
}
myArray = folder.listFiles()? Then simply usemyArray[a].getName()instead?