【发布时间】:2016-11-08 20:03:41
【问题描述】:
在下面的代码中我想使用indexOf(),但我找不到正确的方法。
电影类:
public class Movie {
private String title;
private String genre;
private String actor;
private int yearPublished;
public Movie(String title, String genre, String actor, int yearPublished) {
this.title = title;
this.genre = genre;
this.actor = actor;
this.yearPublished = yearPublished;
}
@Override
public String toString() {
return title + ", Genre: " + genre + ", Actor(s):" + actor + ", Year of publishing: " + yearPublished;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getActor() {
return actor;
}
public int getYearPublished() {
return yearPublished;
}
}
控制器类:
public class Controller {
void start() {
scanning();
printing("Movies:");
selectYourMovie();
}
private List<Movie> movies = new ArrayList<>();
private void scanning() {
try {
Scanner fileScanner = new Scanner(new File("movies.txt"));
String row;
String []data;
while (fileScanner.hasNextLine()) {
row = fileScanner.nextLine();
data = row.split(";");
movies.add(new Movie(data[0], data[1], data[2], Integer.parseInt(data[3])));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void printing(String cim) {
for (Movie movie : movies) {
System.out.println(movie);
}
}
private void selectYourMovie() {
System.out.println(movies.indexOf(/*What to put here?*/);
}
}
以及txt文件的内容:
The Matrix;action;Keanu Reeves;1999
The Lord of the Rings;fantasy;Elijah Wood;2001
Harry Potter;fantasy;Daniel Radcliffe;2001
The Notebook;drama;Ryan Gosling;2004
Step Up;drama, dance;Channing Tatum;2006
Pulp Fiction;crime;Samuel L. Jackson, John Travolta;1994
Star Wars: A New Hope;action;Mark Hamill;1977
所以我想返回给定电影的索引,但我找不到如何处理包含多个对象的列表。由于传递 txt 的整行是行不通的。
对此有何提示?
【问题讨论】: