如何在给定相应用户输入的csv文件中搜索特定字段?

6xfqseft  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(266)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

两天前关门了。
改进这个问题
创建一个类作为每个电视节目类型的父类:家庭、喜剧、黑暗、神秘、其他。
每种类型的电视节目都应从父类继承。确定父类中存在哪些数据和方法,以及每个子类中存在哪些数据和方法每个子类应有两个构造函数。在main()程序中,将所有电视节目存储在一个数组中使用while循环询问用户想要问什么节目,然后回答与电视节目相关的问题。使用arraylist回答问题。向用户提供显示所有电视节目和相应数据的能力。
到目前为止,我已经为每种类型的节目编写了一个超类和子类,并将电视节目存储在一个数组列表中。其他的事我都受不了了。
tvshowdata.csv示例:

Title,audience ,network,actor 1,actor 2,actor 3,actor 4,TV Show Type
The Walking Dead,11.24,AMC,Norman Reedus,Andrew Lincoln,Lauren Cohan,Steven Yeun,Dark
Teen Wolf,3.02,MTV,Dylan O'Brien,Tyler Posey,Holland Roden,Tyler Hoechlin,Dark
Scanner keyboard = new Scanner(System.in);
  String file = "tvshowData.csv";
  List<List<String>> tvShows = new ArrayList<>();
  String line;
  String userInput;

  try (BufferedReader br = new BufferedReader(new FileReader(file))) {
      while ((line = br.readLine()) != null) {
          String[] shows = line.split(",");
          tvShows.add(Arrays.asList(shows));
     }
     System.out.println(tvShows);
  }
  System.out.println("Enter the TV Show you want to know about:");
  userInput = keyboard.nextLine();

// Superclass
public class TVShows
{
   private String title;
   private double audience;
   private String network;
   private String actor1, actor2, actor3, actor4;

   public TVShows(String title, double audience, String network)
   {
      this.title = title;
      this.audience = audience;
      this.network = network;
   }

   public TVShows(String actor1, String actor2, String actor3, String actor4)
   {
      this.actor1 = actor1;
      this.actor2 = actor2;
      this.actor3 = actor3;
      this.actor4 = actor4;
   }

   public String getTitle()
   {
      return title;
   }

   public double getAudience()
   {
      return audience;
   }

   public String network()
   {
      return network;
   }

   public String getActor1()
   {
      return actor1;
   }

   public String getActor2()
   {
      return actor2;
   }

   public String getActor3()
   {
      return actor3;
   }

   public String getActor4()
   {
      return actor4;
   }

}

// Subclass
public class Dark extends TVShows
{
    private String tvShowType;

    public Dark(String nameOfShow, double audience, String network)
    {
        super(nameOfShow, audience, network);
    }

    public Dark(String actor1, String actor2, String actor3, String actor4)
    {
        super(actor1, actor2, actor3, actor4);
    }

    public void setTvShowType(String tvShowType)
    {
        this.tvShowType = tvShowType;
    }

    public String getTvShowType()
    {
        return tvShowType;
    }
}
laik7k3q

laik7k3q1#

主要是你可以使用类似的东西,但是你必须适应你的输入。
匹配是基于使用各种 predicates ```
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Test {

public static void main(String[] args) {

    test();
}

public static void test() {
    //prepare data, here is just something for test 
    List<String> lst1 = Arrays.asList("A", "B", "C", "B", "B");
    List<String> lst2 = Arrays.asList("A", "D", "E");
    ShowData sd1 = new Test().new ShowData("show1", lst1);
    ShowData sd2 = new Test().new ShowData("show2", lst2);
    // System.out.println(sd1);
    ShowList<ShowData> sl = new Test().new ShowList<ShowData>();
    sl.add(sd1);
    sl.add(sd2);

    //write searching criteria
    Predicate<String> pactorA = t -> t.equals("A");
    Predicate<ShowData> haveActorA = t -> t.actors.stream().filter(pactorA).count() > 0;
    System.out.println("[Count Lines for Actor A]=" + sl.stream().filter(haveActorA).count());
    //use criteria and print any matching-lines
    sl.stream().filter(haveActorA).forEach(System.out::println);

    //same as above but with other criteria[another actor]
    Predicate<String> pactorB = t -> t.equals("B");
    Predicate<ShowData> haveActorB = t -> t.actors.stream().filter(pactorB).count() > 0;
    System.out.println("[Count Lines for Actor B]=" + sl.stream().filter(haveActorB).count());
    sl.stream().filter(haveActorB).forEach(System.out::println);

    Predicate<String> pactorF = t -> t.equals("F");
    Predicate<ShowData> haveActorF = t -> t.actors.stream().filter(pactorF).count() > 0;
    System.out.println("[Count Lines for Actor F]=" + sl.stream().filter(haveActorF).count());
    sl.stream().filter(haveActorF).forEach(System.out::println);

}

@SuppressWarnings("hiding")
class ShowList<ShowData> extends ArrayList<ShowData> {
    private static final long serialVersionUID = 1L;

}

class ShowData {
    String name;
    List<String> actors = new ArrayList<>();

    public ShowData(String name, List<String> actor) {
        this.name = name;
        this.actors.addAll(actor);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getActors() {
        return actors;
    }

    public void setActors(List<String> actors) {
        this.actors = actors;
    }

    public String toString() {
        return "name=" + name + " and actors=[" + actors.stream().collect(Collectors.joining(",")) + "]";
    }

}

}

输出:

[Count Lines for Actor A]=2
name=show1 and actors=[A,B,C,B,B]
name=show2 and actors=[A,D,E]

[Count Lines for Actor B]=1
name=show1 and actors=[A,B,C,B,B]

[Count Lines for Actor F]=0

相关问题