如果状态返工,忽略案例澄清

时间:2014-10-18 22:31:36

标签: java if-statement arraylist ignore-case

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

以下是当前代码:

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

1 个答案:

答案 0 :(得分:0)

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

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.")
}