2013-11-26 117 views
1

我試圖在arrayList中搜索由用戶輸入指定的字符串,然後將該搜索的結果打印到控制檯。我知道我應該使用toIndex(),但我無法弄清楚它的語法。在ArrayList中搜索字符串並打印字符串

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(); 
    } 
} 

編輯:主類是MP3_catalogue其中包含arrayLists。無需做特殊的事情,他們具有相同的索引值作爲藝術家

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; 

     } 
    } 
} 

}其他的ArrayList

+0

使用正則表達式和模式匹配器。 –

+0

'ArrayList'在哪裏? –

+0

請添加完整的代碼。添加MP3_catalogue課程詳細信息 – anon

回答

1

這看起來錯

int SV = search.indexOf(search); 

要從MP3_catalogue獲取對象的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); 
} 

因爲我不知道你的數據結構,我不能說是否a博韋方法也可以確定標題和持續時間。

2

這就夠了。不需要for循環..

 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"); 
     } 
0

for循環在你的代碼中的用法是什麼?

也許你可以改變你的代碼是這樣的:

 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)); 
     } 
+0

爲什麼要將i到SV。這只是錯誤的編碼 –

+0

是的你是對的,SV變量是額外的,我只是改變你的代碼來顯示循環的用法。 – ali4j

1

我寧願寫這樣一來,在單封裝豆整個cataloge和訪問/有效利用。

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()); 
      } 
     } 
    } 
}