您好,我在运行这段代码时遇到错误
Map<String, Integer> map = new TreeMap<String, Integer>();//Already populated
public ArrayList<Map<String, Integer>> storage(){
for (String getCharacter : map.keySet()){
String convertBinGrp;
**Integer binGroups = Integer.valueOf(getCharacter);**
if ( binGroups>=0 && binGroups<= 32){
convertBinGrp = Long.toString(binGroups);
binInsideList.get(convertBinGrp);
Integer getVal = getCharacter.getValue();
binInsideList.put(convertBinGrp, getVal);
}
}
错误消息:线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“”
指示的错误是 Integer binGroups = Integer 。 valueof(getCharacter) 类型转换。
我已经尝试使用 long 和 double 来确保它的大小更大,因为我知道它是从 Map 中获取的字符串值。但是有没有其他方法可以解决这个问题呢?我想将字符串转换为数值,以便我可以将其放入适当的容器或组中。谢谢
最佳答案
您的 map 包含 ""
作为键之一。代码试图将 ""
解释为数字,但它不是有效数字。
您需要弄清楚空间的来源,然后决定最佳行动方案(解决导致它的问题、忽略它等)。
关于java - 如何获取 Hashmap Key 并将其转换为其他数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625189/