user3182681
user3182681

Reputation: 1

how to call method that contains array

Complete beginner here. I am trying to call a method from a class to run in the main class. But I cannot seem to figure out why it is not work. Here is how I am calling the class

public static void main(String[] args) {
      findMP3Tracks FT = new findMP3Tracks();
      FT.findMP3();

This is the class method i want to call

public static List<String> findMP3(String p)
{
    List<String> foundMP3Tracks = new ArrayList<>();

    try (DirectoryStream<Path> folderContents = Files.newDirectoryStream(Paths.get(p))) 
    { 
        //Look for 
        for (Path folderItem: folderContents){ 
 if(Files.isDirectory(folderItem, LinkOption.NOFOLLOW_LINKS)) {
                foundMP3Tracks.addAll(findMP3(folderItem.toString()));
            }
            else if(isValidMP3(folderItem.toString())) {
                foundMP3Tracks.add(folderItem.toString());
            }
        }
    } 

Upvotes: 0

Views: 93

Answers (4)

Hrishikesh
Hrishikesh

Reputation: 2053

The method findMP3 is declared as static. Static variables and methods are members of the class.

You can invoke it directly using the classname. So, it should be findMP3Tracks.findMP3()

Also, a word about the static method. If you do know that the behaviour of the method isnt different for different instances, then you might/can declare it as static. Although, it is a design decision that you would make. If it does behave differently based on the passed in parameter, it is better off to have a method which isnt static.

Upvotes: -1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

You've declared the method as

public static List<String> findMP3(String p)

which means (among other things) that when you call it, you're going to pass in a String argument. But you wrote

FT.findMP3();

where you're not passing in any argument at all. Something like

FT.findMP3("Hello");

would compile; or if you had a String variable whose value was the name of the MP3 that you wanted to search for, you could use that too. But the call to any method MUST match the declaration of that method.

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

Assuming findMP3(String) is a method inside the class findMP3Tracks (I would recommend you to follow Java conventions for class names), you may call it in the main as:

public static void main(String[] args) {
      ...
      List<String> result = findMP3Tracks.findMP3("Name of MP3");
}

You may use the name of the class findMP3Tracks to invoke the method findMP3, because it is declared as static. So it's not necessary to create an instance to call it. (Of course you may want to create instances of that class for other operations)

Also, since findMP3 is returning a List<String>, you may want to store it in a variable (In the example result)

Upvotes: 3

m0skit0
m0skit0

Reputation: 25873

First, you don't need instances to call a static method, so this line

findMP3Tracks FT = new findMP3Tracks();

is useless, you can remove it.

Use (for example)

findMP3Tracks.findMP3("Some MP3 name");

Also you need to get the returned value, so it should be:

final List<String> mp3List = findMP3Tracks.findMP3("Some MP3 name");

PS: in Java by convention class names start with uppercase, I suggest you change findMP3Tracks class name to FindMP3Tracks

Upvotes: 0

Related Questions