有没有办法检索具有相同名称但不同评分的所有电影数据?

huangapple go评论79阅读模式
英文:

Is there anyway I could retrieve the data of all the films that have the same name but different scores?

问题

当我运行程序并选择按分数搜索时,相同的标题会打印出相同的分数,而不是打印其他标题。

private void searchReviewScore() throws IOException {
    int review = getNumberInput(_scanner, 1, 5, "输入最低评分 (1 - 5): ");

    // 从文件中获取分数列表。
    Path path = Paths.get("scores.txt");
    List<String> allScores = Files.readAllLines(path);

    for (String movieScore : allScores) 
    {
        int movieScoreInt = Integer.parseInt(movieScore);
        if (review <= movieScoreInt) { 
            int i = allScores.indexOf(movieScore);
            presentMovieAndScore(i);
        }
    }
    //TODO: 根据输入调用搜索电影数据库

    //TODO: 向用户呈现结果

}

/**
 * @param i
 * @throws IOException
 */
private void presentMovieAndScore(int i) throws IOException {
    Path movies = Paths.get("movies.txt");
    List<String> allMovies = Files.readAllLines(movies);

    Path path = Paths.get("scores.txt");
    List<String> allScores = Files.readAllLines(path);

    System.out.println("标题: " + allMovies.get(i) + " 评分: " + allScores.get(i));
}

movie.txt 文件的内容如下:

bones
matrix
deceptions
suits
bones
Gladiator

score.txt 文件的内容如下:

1
2
5
3
4
1
英文:

When I run the program and choose to search by score, the same title is printed for the same score instead of printing the other titles

  private void searchReviewScore() throws IOException {
    int review = getNumberInput(_scanner, 1, 5, &quot;Enter minimum rating (1 - 5): &quot;);

    // Get the list of scores from the file.
    Path path = Paths.get(&quot;scores.txt&quot;);
    List&lt;String&gt; allScores = Files.readAllLines(path);

    for (String movieScore : allScores) 
    {
        int movieScoreInt = Integer.parseInt(movieScore);
        if (review &lt;= movieScoreInt) { 
            int i = allScores.indexOf(movieScore);
            presentMovieAndScore(i);
        }
    }
    //TODO: Add call to search movie database based on input

    //TODO: Present results to user

}

Here is my Print method.

/**
 * @param i
 * @throws IOException
 */
private void presentMovieAndScore(int i) throws IOException {
    Path movies = Paths.get(&quot;movies.txt&quot;);
    List&lt;String&gt; allMovies = Files.readAllLines(movies);

    Path path = Paths.get(&quot;scores.txt&quot;);
    List&lt;String&gt; allScores = Files.readAllLines(path);

    System.out.println(&quot;Titel: &quot; + allMovies.get(i) + &quot; rating: &quot; + allScores.get(i));
}

Here is the content of movie.txt bones
matrix
deceptions
suits
bones
Gladiator

the score.txt contains: 1
2
5
3
4
1

答案1

得分: 2

我猜你的问题出在这行代码上:

int i = allScores.indexOf(movieScore);

indexOf方法总是返回第一个匹配项。所以如果你的表格中有相同的值在不同位置出现,比如在表格的1和5位置,它总是会返回1。在这种情况下,如果你想要元素的位置,使用for each循环并不是一个好主意,尝试使用带索引的for循环。

private void searchReviewScore() throws IOException {
    int review = getNumberInput(_scanner, 1, 5, "Enter minimum rating (1 - 5): ");

    // 从文件中获取分数列表。
    Path path = Paths.get("scores.txt");
    List<String> allScores = Files.readAllLines(path);

    for (int i = 0; i < allScores.size(); i++) {
        String movieScore = allScores.get(i);
        int movieScoreInt = Integer.parseInt(movieScore);
        if (review <= movieScoreInt) {
            presentMovieAndScore(i);
        }
    }
}
英文:

I guess your problem is with this line of code :

> int i = allScores.indexOf(movieScore);

indexOf method returns always first match.
So if your table has same value twice in different positions in table example at 1 and 5 ,it will allways return 1.In this case ,since you want the position of the element using for each loop is not good idea ,try to use index for loop.

private void searchReviewScore() throws IOException {
int review = getNumberInput(_scanner, 1, 5, &quot;Enter minimum rating (1 - 5): &quot;);

// Get the list of scores from the file.
Path path = Paths.get(&quot;scores.txt&quot;);
List&lt;String&gt; allScores = Files.readAllLines(path);

for ( int i=0; i&lt;allScores.size() ;i++) 
{   
    String movieScore =allScores.get(i);
    int movieScoreInt = Integer.parseInt(movieScore);
    if (review &lt;= movieScoreInt) { 
        presentMovieAndScore(i);
    }
}

}

答案2

得分: 1

问题在于你选择了给定分数的第一个名字。所以当你看到1时,你使用它来索引你的电影,得到了 bones,但你实际上想要的是 gladiator。我建议将它们放在一个 TreeMap&lt;Integer,List&lt;String&gt;&gt; 中,其中 keyscorevalue 是该分数的电影列表。

另一种可能性是创建一个 Movie 类并在类中存储名称和分数。如果您以后想要包括更多关于电影的信息,这也将是有益的。

如下所示,TreeMap 提供了一些优势。

TreeMap 允许您根据键的范围选择 subMaps。如何填充该地图取决于您。您还可以选择为具有该评级的电影列表选择明确的分数。

Scanner readMovies = null;
Scanner readScores = null;
try {
readMovies = new Scanner(new File("f:/movies.txt"));
readScores = new Scanner(new File("f:/scores.txt"));
} catch (FileNotFoundException fne) {
	fne.printStackTrace();
}
// 读入值。
// 键是分数。
// 值是具有该分数的电影列表。

TreeMap<Integer, List<String>> data = new TreeMap<>();
while (readMovies.hasNext()) {
	data.computeIfAbsent(readScores.nextInt(),
			k -> new ArrayList<>()).add(readMovies.next());
}

// 提示输入评分

int minRating = 1;
int maxRating = 3;
// 现在,作为示例,获取在地图中存在的评分范围内的电影列表。
Map<Integer, List<String>> subMap = data.subMap(data.ceilingKey(minRating),
		data.floorKey(maxRating));
// 然后将它们打印出来。
subMap.entrySet().forEach(System.out::println);

在这种情况下,这将打印出:

1=[bones, Gladiator]
2=[matrix]

由于没有3的评级,它在2处停止。

英文:

The problem is that you are chosing the first name for a given score. So when you see 1, you use that to index into your movies and get bones when you really want gladiator. I recommend puting them in a TreeMap&lt;Integer,List&lt;String&gt;&gt; where the key is the score and the value is a list of movies of that score.

Another possibility would be to create a Movie class and store the name and score in the class. That would also be beneficial if you ever want include more information about the movie.

A TreeMap offers some advantages as demostrated below.

The TreeMap lets you chose subMaps base on ranges of the keys. How you populate that map is up to you. You can also choose explicit scores for a list of movies at that rating.

Scanner readMovies = null;
Scanner readScores = null;
try {
readMovies = new Scanner(new File(&quot;f:/movies.txt&quot;));
readScores = new Scanner(new File(&quot;f:/scores.txt&quot;));
} catch (FileNotFoundException fne) {
	fne.printStackTrace();
}
// read in the values.
// The key is the score.
// the value is a list of movies that have that score.

TreeMap&lt;Integer, List&lt;String&gt;&gt; data = new TreeMap&lt;&gt;();
while (readMovies.hasNext()) {
	data.computeIfAbsent(readScores.nextInt(),
			k -&gt; new ArrayList&lt;&gt;()).add(readMovies.next());
}

//prompt for rating

int minRating = 1;
int maxRating =  3;
// Now as an example, get a list of movies in the range
// of the ratings that exist in the map.
Map&lt;Integer,List&lt;String&gt;&gt; subMap = data.subMap(data.ceilingKey(minRating),
		data.floorKey(maxRating));
// And print them out.
subMap.entrySet().forEach(System.out::println);

In this case this prints.

1=[bones, Gladiator]
2=[matrix]

Since there are no ratings of 3, it stopped at 2.

huangapple
  • 本文由 发表于 2020年8月10日 22:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63342289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定