Java Hashmap上的ClassCastException到Integer

Java Hashmap上的ClassCastException到Integer,java,classcastexception,Java,Classcastexception,有人知道如何修复这个ClassCastException错误吗?我得到: 线程“main”java.lang.ClassCastException中的“异常”:java.util.HashMap$项不能转换为java.lang.Integer 我的问题是,我以为我是在那个位置调用整数,但显然不是?这项作业将在2小时后到期,因此任何帮助都将不胜感激。评论应该会告诉我们发生了什么 public class WhyHellothere { public static void main(String[

有人知道如何修复这个ClassCastException错误吗?我得到: 线程“main”java.lang.ClassCastException中的“异常”:java.util.HashMap$项不能转换为java.lang.Integer

我的问题是,我以为我是在那个位置调用整数,但显然不是?这项作业将在2小时后到期,因此任何帮助都将不胜感激。评论应该会告诉我们发生了什么

public class WhyHellothere {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    process(s);
}

public static void process(Scanner s) {
    HashMap hashmapofpricing = new HashMap();
    HashMap hashmapofcount = new HashMap();
    for (int i = 0; i < 1; i = 0) {
    String itemDescription;
        int count;
        double unitPrice;

        if ((itemDescription = s.next()).equals("end")) {
            break;
        }
        count = s.nextInt();
        Integer quantityValue;

        if (hashmapofcount.get(itemDescription) != null) {

            quantityValue = (Integer) hashmapofcount.get(itemDescription);
        } else {

            quantityValue = new Integer(0);
        }

        hashmapofcount.put(itemDescription, new Integer(new Integer(count).intValue()
                + quantityValue.intValue()));
        unitPrice = s.nextDouble() * count;
        Double costValue; 

        if (hashmapofpricing.get(itemDescription) != null) { 
           costValue = (Double) hashmapofpricing.get(itemDescription);
        } else {
            costValue = new Double(0); 
        }

        hashmapofpricing.put(itemDescription, new Double(new Double(unitPrice).doubleValue()
                + costValue.doubleValue()));
    }
    Object itemdescription[] = hashmapofcount.entrySet().toArray();
    Object howmanytimestheitemappears[] = hashmapofcount.entrySet().toArray();
    int countIteration=0;
    Object pricing[] = hashmapofpricing.entrySet().toArray();
    int priceIteration=0;
    Integer runningmaxamount = new Integer(0); 
    for (int i = 0; i < howmanytimestheitemappears.length; i++) {
    int q = (Integer)howmanytimestheitemappears[i];//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<this is where the ClassCastException is. No idea why or how to fix.
        if (q > runningmaxamount.intValue()) {runningmaxamount = q;countIteration = i;
        }
    }
    Double maxcost = new Double(0);
    for (int i = 0; i < pricing.length; i++) {
        Double d = (Double) pricing[i];
        if (d.doubleValue() > maxcost.doubleValue()) {
            maxcost = d;
            priceIteration = i;
        }
    }
    String largestCountItem = (String) itemdescription[countIteration];
    String largestCostItem = (String) itemdescription[priceIteration];
    System.out.println("The largest count item with "
            + runningmaxamount.intValue() + " was: " + largestCountItem);
    System.out.println("The largest total cost item at "
            + maxcost.doubleValue() + " was: " + largestCostItem);
}

}
公共类whyhellother{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
过程;
}
公共静态无效处理(扫描仪){
HashMap hashmapofpricing=新HashMap();
HashMap hashmapofcount=新HashMap();
对于(int i=0;i<1;i=0){
字符串项描述;
整数计数;
双倍单价;
如果((itemsdescription=s.next()).equals(“end”)){
打破
}
count=s.nextInt();
整数数量值;
if(hashmapofcount.get(itemDescription)!=null){
quantityValue=(整数)hashmapofcount.get(itemDescription);
}否则{
quantityValue=新整数(0);
}
hashmapofcount.put(itemDescription,新整数
+quantityValue.intValue());
单价=s.nextDouble()*计数;
双重成本价值;
if(hashmapofpricing.get(itemDescription)!=null){
costValue=(双精度)hashmapofpricing.get(itemDescription);
}否则{
成本值=新的双精度(0);
}
hashmapofpricing.put(itemDescription,new Double(new Double,unitPrice.doubleValue)()
+costValue.doubleValue());
}
Object itemdescription[]=hashmapofcount.entrySet().toArray();
对象HowmanyTimeProfiteAppears[]=hashmapofcount.entrySet().toArray();
int countIteration=0;
对象定价[]=hashmapofpricing.entrySet().toArray();
int=0;
整数runningmaxamount=新整数(0);
for(int i=0;i对象HowmanyTimeProfiteAppears[]=hashmapofcount.entrySet().toArray();
for(int i=0;i
howmanytimespipears[i]
属于HashMap$Entry类型。要获取密钥,需要调用
howmanytimespipears[i].getKey()

阅读。

首先,您的HashMap声明和初始化有问题:

最好给出您在hashmap中存储的类型:

HashMap<String, Integer> hashmapofcount = new HashMap<String, Integer>();
HashMap hashmapofcount=new HashMap();
然后您可以使用这种循环轻松地遍历它:

   for (Map.Entry<String,Integer> entry : hashmapofcount.entrySet()) {
        final String description = entry.getKey();
        final Integer value = entry.getValue();
    }
for(Map.Entry:hashmapofcount.entrySet()){
最终字符串描述=entry.getKey();
最终整数值=entry.getValue();
}

PS:你不需要太多的
装箱
整数和双精度,这会让你的代码看起来有点糟糕。另外,你要添加两个整数,双精度单价和成本值,我想你可能需要使用
单价+“”+costValue
(?)

请帮我们一个忙,删除代码中所有分散注意力的注释。我们越容易阅读和理解您的代码,就越容易帮助您。此外,请务必用明显的注释来表示,这可能是代码中应该包含的唯一注释,哪一行导致了您的问题。您的注释会使代码无法阅读……如果您看看文档,HashMap$Entry和Integer之间唯一共同的父对象是Object。我已经删除了所有凌乱的注释,只有例外的行有注释。我看到了一系列的OK。HowmanyTimesProfiteAppears数组填充了
hashmapofcount.entrySet().toArray()
,因此如果它不包含HashMap条目,那将令人惊讶。您期望得到什么?如果我将其更改为
HowmanyTimesProfiteAppears[i].getKey()
,甚至更改为
HowmanyTimesProfiteAppears[i].getValue()
,则会出现一个错误,提示“类型对象的方法getKey()未定义”“。我该如何解决这个问题?对不起,我是这方面的新手,但希望能学得很快。将
对象HowmanyItemAppears[]
更改为
映射。输入HowmanyItemAppears[]
如果我这样做,它会告诉我Map无法解析为一个类型。你也需要导入
java.util.Map
。我刚刚意识到我没有它,但即使如此,它进一步添加了“类型不匹配,无法从Object[]转换为Map.Entry”这是否意味着我不能使用
entrySet().toArray()
我必须做些不同的事情吗?
   for (Map.Entry<String,Integer> entry : hashmapofcount.entrySet()) {
        final String description = entry.getKey();
        final Integer value = entry.getValue();
    }