Reputation: 298
Hi im creating a simple mp3 database that stores a trackNum, name and duration. I need to search an array list and get the index of the search.
Here is what i have at the moment.
//My methods
public void searchTrackNum(Intager trackNumber){
System.out.println(trackNum + ": " + name[index] + " " + duration[index]);
}
public void searchName(String name){
System.out.println(trackNum + ": " + name[index] + " " + duration[index]);
}
//Using the methods
case 4:
System.out.println("What name would you like to search for: 1-Track Number or 2-Name");
int question = in.nextInt();
if(question == 1){
System.out.println("Please enter the track Number?");
meth.searchTrackNum(in.nextInt());
}
if(question == 2){
System.out.println("Please enter the Name?");
meth.searchName(in.next());
}
else{
System.out.println("Pease enter 1 or 2.");
}
break;
Upvotes: 1
Views: 53
Reputation: 533500
I suggest using
int index = Arrays.asList(names).indexOf(nameToLookFor);
If the names are sorted, you can use binarySearch instead.
Upvotes: 2