0

I have the code attached below to search through an array for a specific string but this code isn't working. Why? if(children[x].equals(music)) does work but only if you type in the full file name (stored in the music variable) how can I make it so that if the value of music is 'h' any element in the children array containing the character 'h' will print as an output?

public class main {

    public static void main(String[] args) throws IOException {
        //text file, should be opening in default text editor   


        //Look through directory
        File dir = new File("/Users/Runa.c/Music/music");
        String[] children = dir.list();

        if (children == null) {
           System.out.println("does not exist or is not a directory");
        } else {
           for (int i = 0; i < children.length; i++) {
              String filename = children[i];
              System.out.println(filename);
           }
        }

        //search
        Scanner search = new Scanner(System.in);
        System.out.print("Search for a song: ");
        String music = search.next();


       for (int x=0; x<children.length; x++){
           if(children[x].equals(music)){
               System.out.println(children[x]);
           }
       }



        //Open file
        File file = new File("/Users/Runa.C/Music/music/youtubnow.co - Distrion & Alex Skrindo - Lightning [NCS Release].mp3");

        Desktop desktop = Desktop.getDesktop();
        if(file.exists()) desktop.open(file);

        file = new File("/Users/Runa.C/Music/music/youtubnow.co - Distrion & Alex Skrindo - Lightning [NCS Release].mp3");
        if(file.exists()) desktop.open(file);
    search.close(); 
    }
}



12
  • 1
    Does changing the condition to if(children[x].equals(music)) has any effect? Commented Jan 2, 2020 at 19:36
  • If children[x] and music are expected to be strings, perhaps using children[x].equals(music) could help? Commented Jan 2, 2020 at 19:36
  • Could you include more code so that we might see what children[x] and music are? Commented Jan 2, 2020 at 19:37
  • 1
    @HrittikChatterjee I hope you mean a String of a .mp3 file? Because no user is going to be able to input an mp3 file... Commented Jan 2, 2020 at 19:42
  • 2
    You could use String's contains method within your loop. This method checks if the given argument is contained within the string on which the method is called. Commented Jan 2, 2020 at 19:48

1 Answer 1

1

Instead of:

if (children[x].equals(music))

You can use:

if (children[x].contains(music))

According to: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

Returns true if and only if this string contains the specified sequence of char values.

Sign up to request clarification or add additional context in comments.

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.