简体   繁体   English

如何修复错误:Map 类型中的 put(Integer, Integer) 方法<integer,integer>不适用于 arguments (int, String)</integer,integer>

[英]How to fix error: The method put(Integer, Integer) in the type Map<Integer,Integer> is not applicable for the arguments (int, String)

I don't know how to fix this.我不知道如何解决这个问题。 I want to intake a certain number of strings with a hashmap.我想用 hashmap 接收一定数量的字符串。 I am still a beginner coder and just learned hashmap.我仍然是初学者,刚刚学习了 hashmap。 PLS help.请帮忙。

public class Hashmap {
public static void main(String[] args) {
    Scanner Scanner = new Scanner(System.in);
    int lines = Scanner.nextInt();
    Map<Integer, Integer> hash = new HashMap<Integer, Integer>();
    for (int i = 0; i < lines; i++) {
        hash.put(i, Scanner.next());
    }
}

} }

Scanner.next returns a String object. Scanner.next返回一个String object。 You cannot put a String as the value in a map you declared as taking Integer objects for values.您不能将String作为值放入您声明为将Integer对象作为值的 map 中。 Square peg, round hole.方钉,圆孔。

You have a choice of three ways to fix.您可以选择三种修复方法。

You could change your map declaration to take String objects as the value.您可以更改您的 map 声明以将String对象作为值。 Change this:改变这个:

Map< Integer , Integer > map = new HashMap<>();

… to: … 至:

Map< Integer , String > map = new HashMap<>();

Or, you can collect integers from the Scanner rather than text.或者,您可以从 Scanner 收集整数而不是文本。 Call Scanner#nextInt .调用Scanner#nextInt

Or, you can collect text from the scanner, and then transform that text to an integer.或者,您可以从扫描仪收集文本,然后将该文本转换为 integer。 Call Integer.valueOf .调用Integer.valueOf

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:ArrayList 类型中的方法 add(Integer)<integer> 不适用于 arguments(字符串)</integer> - ERROR: The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (String) Integer类型的方法valueOf(String)不适用于参数(int) - The method valueOf(String) in the type Integer is not applicable for the arguments (int) 方法put(String,ArrayList <Integer> ),类型为TreeMap <String,ArrayList<Integer> &gt;不适用于参数(字符串,布尔值) - The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean) Integer.parseInt返回错误:Integer类型的方法parseInt(String)不适用于参数(R.string) - Integer.parseInt Returns an error: The method parseInt(String) in the type Integer is not applicable for the arguments (R.string) 错误 java 方法组合Sum(int[], int, List<integer> ) 类型中的解决方案不适用于 arguments (int[], int, boolean)</integer> - error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean) 方法排序(图 <Integer,Object> )不适用于自变量(地图 <Integer,Entity> ) - Method sort(Map<Integer,Object>) is not applicable for the arguments (Map<Integer,Entity>) GenericMethodsAndBoundedTypeParameters 类型中的方法 countGreaterThan(T[], T) 不适用于参数 (Integer[], Integer) - The method countGreaterThan(T[], T) in the type GenericMethodsAndBoundedTypeParameters is not applicable for the arguments (Integer[], Integer) Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值) - The method parseInt(String) in the type Integer is not applicable for the arguments (boolean) 在InnerList类型中不适用于参数(整数) - in the type InnerList is not applicable for the arguments (Integer) Stream <List <Integer >>类型中的max(Comparator <?super List <Integer >>)方法不适用于参数(Comparator <Integer>) - The method max(Comparator<? super List<Integer>>) in the type Stream<List<Integer>> is not applicable for the arguments (Comparator<Integer>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM