Java 就像一个搜索引擎
我有一个对象列表,其中包含一个名为Video的类的电影名称: 我需要让用户输入至少包含3个字符的名称或名称的一部分,然后打印与用户输入匹配的电影,我不确定如何以简单的方式完成 有什么想法吗? 这是我的全部代码Java 就像一个搜索引擎,java,regex,object,match,Java,Regex,Object,Match,我有一个对象列表,其中包含一个名为Video的类的电影名称: 我需要让用户输入至少包含3个字符的名称或名称的一部分,然后打印与用户输入匹配的电影,我不确定如何以简单的方式完成 有什么想法吗? 这是我的全部代码 class Video { String name, director, type, rating; Double price; boolean borrowed; String borrowedTrue; Video(String n, S
class Video
{
String name, director, type, rating;
Double price;
boolean borrowed;
String borrowedTrue;
Video(String n, String d, String t, String r, Double p, boolean b)
{
name = n;
director = d;
type = t;
rating = r;
price = p;
borrowed = b;
if(b == true)
{
borrowedTrue = "Yes";
}
else if(b == false)
{
borrowedTrue = "No";
}
}
public void display()
{
System.out.println("");
System.out.println("******************************");
System.out.println("");
System.out.println("Name: "+name);
System.out.println("Director: "+director);
System.out.println("Type: "+type);
System.out.println("Rating: "+rating);
System.out.println("Prince: "+price);
System.out.println("Borrowed: " +borrowedTrue);
System.out.println("");
System.out.println("******************************");
}
}
对于类,我有这个对象
import java.util.*;
class TestVideo
{
static int counter = 0;
public static void main(String args[])
{
Video v1 = new Video("The Shawshank Redemption","Frank Darabont","Drama","9.2*",49.99,true);
Video v2 = new Video("The Godfather","Francis Ford Coppola","Crime,Drama","9.2*",49.99,true);
Video v3 = new Video("The Godfather: Part II","Francis Ford Coppola","Crime,Drama","9*",48.99,true);
Video v4 = new Video("The Dark Knight","Christopher Nolan","Action,Crime,Drama","9*",48.99,false);
Video v5 = new Video("Pulp Fiction ","Quentin Tarantino","Crime,Thriller,Drama","8.9*",47.99,true);
Video v6 = new Video("12 Angry Men","Sidney Lumet","Drama","8.9*",47.99,true);
Video v7 = new Video("The Good, the Bad and the Ugly","Sergio Leone","Western","8.9*",47.99,false);
Video v8 = new Video("Schindler's List","Steven Spielberg","Bography,History,Drama","8.9*",47.99,false);
Video v9 = new Video("The Lord of the Rings: The Return of the King","Peter Jackson"," Peter Jackson","8.9*",47.99,true);
Video v10 = new Video("Fight Club","David Fincher","Drama","8.9*",47.99,false);
Video v11 = new Video("The Lord of the Rings: The Fellowship of the Ring"," Peter Jackson"," Peter Jackson","8.8*",46.99,true);
Video v12 = new Video("Star Wars: Episode V - The Empire Strikes Back","Irvin Kershner","Action,Adventure,Fanstasy","8.8*",46.99,true);
Video v13 = new Video("Forrest Gump ","Robert Zemeckis","Romance,Drama","8.8*",46.99,false);
Video v14 = new Video("Inception","Christopher Nolan","Action,Mystery,Sci-Fi","8.8*",47.99,false);
Video v15 = new Video("The Lord of the Rings: The Two Towers","Peter Jackson","Adventure,Fantasy","8.8*",45.99,false);
Video v16 = new Video("Interstellar","Christopher Nolan","Adventure,Sci-Fi","8.8*",44.99,false);
Scanner user_input = new Scanner(System.in);
System.out.println("Please enter the name of the movie");
String matchName =user_input.next();
if (matchName.matchesIgnoreCase("[the]*"))
{
v1.display();
v2.display();
// i have to manually search for any videos which name contains 'the'
}
else
{
System.out.println("No match");
}
}
}
如果将视频实例存储在实现java.util.List的类中,那么完成此操作的一个简单算法是搜索列表,并将用户传递的搜索字符串与视频的名称字段进行比较。我会阅读字符串方法的javadocs,比如 例如,假定
// minimum functionality needed for example
public class Video{
private String name;
public String getName(){
return name;
}
public void printDetails(){
// some method that prints the movie details
}
}
您有您的列表,可能作为封闭类的字段:
// Somewhere relevant in your code
List<Video> videos = new ArrayList<Video>();
// add your examples to the list
videos.add(v1);
videos.add(v2);
videos.add(v3);
videos.add(v4);
videos.add(v5);
此方法将解决您的问题:
public void printNames(String query){
if(query.length() < 3){
// report an error, with an exception or print statement
System.out.println("Search query must be at least 3 characters.");
}
for(Video v : videos){
if(v.getName().startsWith(query)){
v.printDetails();
}
}
}
或者,我建议使用StringBuilder构建返回字符串,然后打印它。或者,返回匹配视频的列表,并以其他方式对其进行操作。如果您不知道任何简单的方法,请尝试使用非简单的方法。不管怎样,重要的是你要先尝试。然后,如果您仍然被困在这里,您可以带着一个更好、更具体的问题以及您的代码尝试来到这里。请不要在注释中编写代码。编辑你的问题。
public void printNames(String query){
if(query.length() < 3){
// report an error, with an exception or print statement
System.out.println("Search query must be at least 3 characters.");
}
for(Video v : videos){
if(v.getName().startsWith(query)){
v.printDetails();
}
}
}