2

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();      
    }

}
3
  • 1
    Try doing some debugging, Print out the value of length and the array returned by folder.listFiles(). You said: "it shows the name of the kth image in the folder "Image". But now ... " What did you change to make the code fail now after it used to work? Commented Oct 26, 2015 at 0:04
  • 2
    myArray = folder.listFiles()? Then simply use myArray[a].getName() instead? Commented Oct 26, 2015 at 0:05
  • 1
    move int i=0 to outside the for loop. Commented Oct 26, 2015 at 0:39

2 Answers 2

2

I'm assuming that you intend i to be incremented so that myArray gets filled with the names:

public static void listFilesForFolder(final File folder) {
//put every file name into the array    
    int i = 0;  // Declare outside the loop.
    for (final File fileEntry : folder.listFiles()) {
        myArray[i] = fileEntry.getName();           
    i++;
    }
}

Mind you, this will give an ArrayIndexOutOfBoundsException, because you've initialized myArray to have one element too few:

static int length = (new File("/Users/Sam/Desktop/Image").listFiles().length)-1;

You don't really want to use an array for this, since arrays have a fixed size. A List (e.g. ArrayList) would be a much better fit, and you wouldn't need to worry about indices and lengths:

static List<String> myList = new ArrayList<>();

public static void listFilesForFolder(final File folder) {
//put every file name into the list
    for (final File fileEntry : folder.listFiles()) {
        myList.add(fileEntry.getName());           
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Although I have never used Lists before, but after I went through some documentations this actually works better than array! Thank you very much xP
1

Please try the following code (I have tested it). There are two main points:

  1. Move the int i = 0; before the for loop, to avoid setting i to 0 in every loop.
  2. Remove the "-1" while declaring length, which is not correct and will create ArrayIndexOutOfBoundsException.

    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);
        static String[] myArray = new String[length];
    
        public static void listFilesForFolder(final File folder) {
            // put every file name into the array
            int i = 0;
            for (final File fileEntry : folder.listFiles()) {
                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();
        }
    }
    

5 Comments

Please add an explanation why it works (and what the differences are). Otherwise the answer might be useless for learning or new users that have a similar problem.
@PinkieSwirl Thanks for reminding! I have added the explanations!
No problem :) Now myArray[a] might still cause an ArrayIndexOutOfBoundsException since a is not check to be >= 0 and < myArray.length (but maybe that is ok if correct input can be assumed) and the formatting is a bit off.
@PinkieSwirl Yes you are absolutely right! I adjusted the format, and maybe SamW could try to add some code to check the array size? :-)
Thanks a lot! This really helps me ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.