Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch an arraylist for a string and print the string
    primarykey
    data
    text
    <p>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 <code>toIndex()</code> but I can't work out how to syntax it.</p> <pre><code>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&lt; 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(); } } </code></pre> <p>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</p> <pre><code>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&lt;String&gt; title = new ArrayList&lt;String&gt;(); public static ArrayList&lt;String&gt; artist = new ArrayList&lt;String&gt;(); public static ArrayList&lt;String&gt; duration = new ArrayList&lt;String&gt;(); //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; } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload