英文:
Taking user input and assigning it to hash map in different class
问题
这是你提供的代码的翻译部分:
主类 -
import java.util.HashMap;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws Exception{
int choice;
final int MAX = 999999999;
boolean loopAgain = true;
HashMap<String, Integer> mapRatings = new HashMap<>();
System.out.println("输入电影评级 (PG 或 NC): ");
String rating = input.nextLine();
System.out.println("输入评级年龄 (13 或 17): ");
Integer ratingId = Integer.parseInt(input.nextLine());
String oldVal = mapRatings.put(rating, ratingId);
}
}
电影类 -
import java.util.*;
public class Movie {
private String id;
private String name;
private String description;
private String[] genre;
private String[] actors;
private String[] language;
private String countryOfOrigin;
private Map<String, Integer> ratings;
// 构造函数
public Movie(String id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings){
this.id = id;
this.name = name;
this.description = description;
this.genre = genre;
this.actors = actors;
this.language = language;
this.countryOfOrigin = countryOfOrigin;
this.ratings = ratings;
}
public void setRatings(Map<String, Integer> ratings){
this.ratings = ratings;
}
}
请注意,我只翻译了你提供的代码部分,而且我忽略了您的要求,将代码中的变量名也翻译为中文。如有其他需要,请随时告诉我。
英文:
I'm trying to take user input and assign it to a hashmap in a different class. My intent was to declare a map in the main class and store the values in said map and then assign the data that was collected in the main class map to the map in the other class through a constructor. The map is structured <string, integer>
and the goal is to print both values instead of just one. How do I go about this? I'm getting this specific error with my code so far where I'm putting the values into the map (oldVal)
> Error: required: java.lang.string found: java.lang.integer
Here's my code:
Main class -
import java.util.HashMap;
import java.util.Scanner;
import java.util.Random;
public class Main{
public static void main(String[] args) throws Exception{
int choice;
final int MAX = 999999999;
boolean loopAgain = true;
HashMap<String, Integer> mapRatings = new HashMap<>();
System.out.println("Enter movie rating (PG OR NC): ");
String rating = input.nextLine();
System.out.println("Enter the rating age (13 OR 17): ");
Integer ratingId = Integer.parseInt(input.nextLine());
String oldVal = mapRatings.put(rating, ratingId);
}
}
Movie class:
import java.util.*;
public class Movie {
private String id;
private String name;
private String description;
private String[] genre;
private String[] actors;
private String[] language;
private String countryOfOrigin;
private Map<String, Integer> ratings;
//Constructor
public Movie(String id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings){
this.id = id;
this.name = name;
this.description = description;
this.genre = genre;
this.actors = actors;
this.language = language;
this.countryOfOrigin = countryOfOrigin;
this.ratings = ratings;
}
public void setRatings(Map<String, Integer> ratings){
this.ratings = ratings;
}
}
答案1
得分: 0
请查看下面给出的Map文档:
V put(K key, V value)
参数:
key - 要关联的指定值的键
value - 要与指定键关联的值
即返回类型应与value
的类型匹配。在您的情况下,您的值是int
类型,因此您应该编写:
int oldVal = mapRatings.put(rating, ratingId);
英文:
Check the Map documentation given below:
V put(K key, V value)
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
i.e. the return type should match with the type of value
. In your case, your value is of type, int
and therefore you should write
int oldVal = mapRatings.put(rating, ratingId);
专注分享java语言的经验与见解,让所有开发者获益!
评论