1

I'm trying to search an arrayList for a string specified by user input, then print the result of that search to the console. I understand I should be using toIndex() but I can't work out how to syntax it.

import java.util.*;

public class searchSongs {

    public static void main(String[]args){

        Scanner searchBar = new Scanner(System.in);
        System.out.println("Enter song title");
        String search = searchBar.nextLine().toUpperCase();

            for (int i = 0; i< MP3_catalogue.artist.size(); i++){
                if (MP3_catalogue.artist.contains(search)){
                    int SV = search.indexOf(search);
                    System.out.println(MP3_catalogue.title.get(SV));
                    System.out.println(MP3_catalogue.artist.get(SV));
                    System.out.println(MP3_catalogue.duration.get(SV));
            }
        }

        MP3_catalogue obj = new MP3_catalogue();
    }
}

EDIT: The main class is MP3_catalogue which contains the arrayLists. No need to do anything special with the other arrayLists they have the same index values as artist

import java.util.*;

public class MP3_catalogue {
    static String gotoMenu = new String();

//"gotoMenu" is variable used to return to menu after each interaction with the methods.

public static ArrayList<String> title = new ArrayList<String>(); 
public static ArrayList<String> artist = new ArrayList<String>();
public static ArrayList<String> duration = new ArrayList<String>();

//arrayLists for song elements

@SuppressWarnings("resource")
public static void main(String[]args){
    System.out.println("Welcome to your music catalogue. \n \n" + "Menu choices:");
    System.out.println("A to add songs\n" + "D to delete songs\n" + "S to search catalogue\n" + "C to change song\n" + "? to shuffle catalogue\n");

    gotoMenu = "Y"; 
    while (gotoMenu.equals("Y")){
    System.out.println("Enter your choice:"); 

    Scanner userOption = new Scanner(System.in); //scanner to choose menu option
    String choice = userOption.nextLine().toUpperCase();

        switch (choice) {//switch statement used to go to each menu option

        case "A": addSongs.main(args);//executes addSongs
            System.out.println("Would you like to return to menu? Press Y to return, press N to exit program.");//choice to return to menu
            String goback = userOption.nextLine().toUpperCase();
            if(goback.equals("N"))
            {
                gotoMenu = "N";
            }
            break;

        case "D": deleteSongs.main(args);
            System.out.println("Would you like to return to menu? Press Y to return, press N to exit program.");//choice to return to menu
            String returnMenu = userOption.nextLine().toUpperCase();
            if(returnMenu.equals("N"))
            {
            gotoMenu = "N";
            };
            break;

        case "S": searchSongs.main(args);
                    gotoMenu = "N";
            break;
        case "C": System.out.println("Change songs");
                    gotoMenu = "N";
            break;
        case "?": System.out.println("Shuffle time");
                    gotoMenu = "N";
            break;
        default: System.out.println("Doesn't match a menu choice. Type more carefully this time.");
            break;

        }
    }
}

}

5
  • Use regex and pattern matchers. Commented Nov 26, 2013 at 8:29
  • Where is ArrayList? Commented Nov 26, 2013 at 8:30
  • Please add complete code . Add MP3_catalogue classs details Commented Nov 26, 2013 at 8:30
  • What do you mean by search.indexOf(search);? Commented Nov 26, 2013 at 8:32
  • stackoverflow.com/questions/2642589/… this might help understanding how the contains() method works Commented Nov 26, 2013 at 8:58

4 Answers 4

2

this much enough. no need to for loop..

        if (MP3_catalogue.artist.contains(search)){
                int SV = MP3_catalogue.artist.indexOf(search);
                System.out.println(MP3_catalogue.title.get(SV));
                System.out.println(MP3_catalogue.artist.get(SV));
                System.out.println(MP3_catalogue.duration.get(SV));
        } else {
               System.out.println("not found");
        }
Sign up to request clarification or add additional context in comments.

Comments

1

This looks wrong

int SV = search.indexOf(search);

You want to get the object from MP3_catalogue in the loop

for (int i = 0; i< MP3_catalogue.artist.size(); i++){
       Artist artist = MP3_catalogue.artist.get (i);
            if (artist.contains(search)){
                System.out.println(artist);
}

As I do not know your data structure, I can not say whether the above approach would also be OK for title and duration.

Comments

1

I would prefer to write this way , encapsulate entire cataloge in to single bean,and access/use effectively .

class MP3Catalogue {
    private String title;
    private String artist;
    private String duration;

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public String getDuration() {
        return duration;
    }
}



public class SearchSongs {
    public static ArrayList<MP3Catalogue> catelogs =  new ArrayList<MP3Catalogue>();
    public static void main(String[] args) {
        Scanner searchBar = new Scanner(System.in);
        System.out.println("Enter song title");
        String search = searchBar.nextLine().toUpperCase();

        for (MP3Catalogue cat : catelogs) {
            if (cat.getArtist().equalsIgnoreCase(search)) {
                System.out.println(" Title = " + cat.getTitle() +" Duration = " + cat.getDuration());
            }
        }
    }
}

Comments

0

what's the usage of for loop in your code?

maybe you can change your code like this:

        for (int i = 0; i< MP3_catalogue.artist.size(); i++){
            if (MP3_catalogue.artist.get(i).equals(search)){
                int SV = i;
                System.out.println(MP3_catalogue.title.get(SV));
                System.out.println(MP3_catalogue.artist.get(SV));
                System.out.println(MP3_catalogue.duration.get(SV));
        }

2 Comments

why assign i to SV. This is just bad coding
Yes you are right, the SV variable is additional, I just change your code to show the usage of for loop.

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.