Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果状态为返工,则忽略案例说明_Java_If Statement_Arraylist_Ignore Case - Fatal编程技术网

Java 如果状态为返工,则忽略案例说明

Java 如果状态为返工,则忽略案例说明,java,if-statement,arraylist,ignore-case,Java,If Statement,Arraylist,Ignore Case,所以,我有一个类,它创建了一个包含标题的Song对象:Artister:Album。 我提示用户询问特定的艺术家,然后从包含主播放列表的ArrayList中,程序返回每个特定艺术家的列表,按标题排序。这没问题。我遇到的问题是,用户请求的艺术家不在主播放列表中。当我使用if/then/else进行编码时,对于提示的艺术家与主播放列表中的艺术家不匹配的每种情况,我都会收到一个Sysout。此外,当用户输入正确的艺术家时,将生成正确的格式化Arraylist,以及与提示名称不匹配的每个艺术家的Syso

所以,我有一个类,它创建了一个包含标题的Song对象:Artister:Album。 我提示用户询问特定的艺术家,然后从包含主播放列表的ArrayList中,程序返回每个特定艺术家的列表,按标题排序。这没问题。我遇到的问题是,用户请求的艺术家不在主播放列表中。当我使用if/then/else进行编码时,对于提示的艺术家与主播放列表中的艺术家不匹配的每种情况,我都会收到一个Sysout。此外,当用户输入正确的艺术家时,将生成正确的格式化Arraylist,以及与提示名称不匹配的每个艺术家的Sysout,因此,整个主列表基本上是完整的。我需要返回一个格式化的ArrayList,其中只包含提示的艺术家,或者返回一条语句,例如,列表中找不到艺术家。我已经被困了几个小时,如果你愿意的话,我需要一些新的想法。我知道为什么会发生这种情况,但我就是想不出如何绕过我的预期输出。另外,在理解为什么ignoreCase不为我检查SearchArtister与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;
   }
}
解决方案:如果存在匹配项,则添加到结果列表中。如果结果列表为空,则打印艺术家不存在

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

非常感谢。我盯着我的代码看了很长时间,所以我忽略了这个简单的概念。输出如预期的那样。再次感谢你。