【问题标题】:If state rework, ignore-case clarification如果状态返工,忽略案例说明
【发布时间】:2014-10-18 22:31:35
【问题描述】:

所以,我有一个类可以创建一个包含标题的歌曲对象:艺术家:专辑。 我提示用户询问特定艺术家,然后从包含主播放列表的 ArrayList 中,程序为每个特定艺术家返回一个按标题排序的列表。这没问题。我遇到的问题是用户要求的艺术家不在主播放列表中。当我使用 if/then/else 对此进行编码时,对于提示的艺术家与主播放列表中的艺术家不匹配的每种情况,我都会收到一个 Sysout。此外,当用户输入正确的艺术家时,会生成正确的、格式化的 Arraylist,以及与提示名称不匹配的每个艺术家的 Sysout(因此,基本上是整个主列表)。我需要返回一个格式化的 ArrayList,其中只包含提示的艺术家,或者一个语句,例如“列表中未找到艺术家”。我已经被困了几个小时,如果你愿意的话,我需要一些新的想法。我知道为什么会这样,我只是想不出我想要的输出。此外,在理解为什么 ignoreCase() 对我不起作用(用于检查 searchArtist 与 Song 对象的实例变量)方面会有很大帮助。

下面是当前代码:

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;


public class SongList {


  public static void main(String[] args){


  //An arraylist theList to accept a file containg title : artist : album
    ArrayList<Song> theList = new ArrayList<Song>();


  try{
    Scanner in = new Scanner(System.in);
    File inputFile;
   // Prompts user for proper input
      do{ System.out.println("Please enter a valid input file.");
          String input = in.next();
          inputFile = new File(input);
     }while(!inputFile.exists());


    Scanner inp = new Scanner(new FileReader(inputFile));

   String line = ""; 
   //Accepts a line that is greater in length that 2 (it assumes a colon, and one blank space)
   while((inp.hasNextLine()))
   {
     line = inp.nextLine();
     line = line.trim();
     if(line.length() > 2){
       Song n = createSong(line);
       theList.add(n);
     }
   }
  }

   catch(Exception e){
     System.out.println("Error with the input file: " + e.getMessage());
   }

   Collections.sort(theList); //Sorts by title

   //An arrayList particularArtist that creates an arrayList of a specified artist as given by the     user
   ArrayList<Song> particularArtist = new ArrayList<Song>(); 
   Scanner sa = new Scanner(System.in);
   String searchArtist = "";

   System.out.print("Please enter the name of an artist you'd like to find.");
   searchArtist = sa.next();
//This is where I am having the issue.
    for(Song theArtist : theList)
      if(theArtist.getArtist().contains(searchArtist))
    {
      particularArtist.add(theArtist);
    }
    else{System.out.println("The artist you are looking for does not exist in the play list.");}

    for(Song is : particularArtist)
    System.out.println(is);


  }
   /*
    * Method for creating a Song object given an input file. In the format "Title : Artist: Album,"     substrings
    * are created at the colons, and white space is trimmed.
    */

   public static Song createSong(String a) {
    int index1 = a.indexOf(':');
    int index2 = a.indexOf(':', index1 + 1);
    Song s = new Song(a.substring(0, index1).trim(), a.substring(index1 + 1, index2).trim(),     a.substring(index2 + 1).trim());
    return s;
   }
}

【问题讨论】:

    标签: java if-statement arraylist ignore-case


    【解决方案1】:

    解决方案:如果匹配存在则添加到结果列表(特定艺术家)。如果结果列表为空,则打印艺术家不存在。

    for(Song theArtist : theList) {    
      if(theArtist.getArtist().contains(searchArtist)) {  
          particularArtist.add(theArtist);  
      }
    }  
    
    for(Song is : particularArtist) {
      System.out.println(is);
    }
    
    if (particularArtist.size() == 0) {
      System.out.println("The artist you are looking for does not exist in the play list.")
    }
    

    【讨论】:

    • 非常感谢。我盯着我的代码看了很长时间,以至于忽略了这个简单的概念。输出符合预期。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多