2

I have a method which iterates over a supplied array of directories and prints out all of the audio files in there. I want to add a counter to get a total of all audio files found from the total scan. At the moment, whenever the

if(file.isDirectory()){new MusicGetter(path).lookup();}

condition is met and it moves onto a new folder, the total is reset.

Thanks for any help :)

full method:

public void lookup()
{     
    File folder = new File(c);       
    //File[] listOfFiles = folder.listFiles();
    int count = 0;

    if(folder.listFiles() == null)
    {            
        return;            
    }

    for (File file : folder.listFiles())
    {
        String path = file.getPath();

        //in each directory print out the audio files
        if(path.contains(".mp3") || path.contains(".wav") || path.contains(".flac") || path.contains(".m4a") || path.contains(".ogg") || path.contains(".wma"))
        {
            System.out.println(path);   
            count++;                             
        }            

        //call method again if the file is a directory
        if (file.isDirectory())
        {
            new MusicGetter(path).lookup();               
        }           
    }        
    System.out.println("count is" + count);        
}

4 Answers 4

2

I would change the method from void to return type int and do it as follows:

 public int lookup()
    {     
        File folder = new File(c);       
        //File[] listOfFiles = folder.listFiles();
        int count = 0;

        if(folder.listFiles() == null)
        {            
            return count;            
        }

        for (File file : folder.listFiles())
        {
            String path = file.getPath();

            //in each directory print out the audio files
            if(path.contains(".mp3") || path.contains(".wav") || path.contains(".flac") || path.contains(".m4a") || path.contains(".ogg") || path.contains(".wma"))
            {
                System.out.println(path);   
                count++;                             
            }            

            //call method again if the file is a directory
            if (file.isDirectory())
            {
                count += new MusicGetter(path).lookup();               
            }           
        }
        return count;     
        System.out.println("count is" + count);        
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@brainZap You might want to check what happens if I have a directory named music.mp3 :)
0

You either need to store the count in an object and pass the object through the method calls so that you can update the count in it or you need to store the count in a member variable somewhere. Be aware that in the second case you risk your code being non-re-entrant as two counts at the same time will interfere with each other's results.

Comments

0

Change it from void to int for the return like so:

public int lookup()

and when you call the recursive part, do this:

count += new MusicGetter(path).lookup();

So add to the count after each file, and return the count in the recursive part.

Comments

0

Don't create a new MusicGetter - change it to .lookup(File file); and pass it the file.

Why are you converting the File to a string path, anyway?

Edit

Here's what I mean:

public class MusicGetter {

    private final File root;
    private int count = 0;

    public MusicGetter(File root) {
        this.root = root;
    }

    public void lookup() {
        lookup(root);
        System.out.println("count is: " + count);
    }

    private void lookup(File folder) {

        if (folder == null)
            return;

        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                lookup(file);
            } else if (file.getName().matches(".*\\.(mp3|wav|flac|m4a|ogg|wma)$")) {
                System.out.println(file);
                count++;
            }
        }
    }

}

Comments

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.