Hashmap到Integer的ClassCastException

时间:2014-09-06 02:00:13

标签: java classcastexception

有谁知道如何修复此ClassCastException错误?我明白了: “线程中的异常”主“java.lang.ClassCastException:java.util.HashMap $ Entry无法强制转换为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);
}

}

2 个答案:

答案 0 :(得分:0)

Object howmanytimestheitemappears[] = hashmapofcount.entrySet().toArray();
for (int i = 0; i < howmanytimestheitemappears.length; i++) {
    int q = (Integer)howmanytimestheitemappears[i];/
....
}

howmanytimestheitemappears[i]的类型为HashMap $ Entry。要获得密钥,您需要致电howmanytimestheitemappears[i].getKey() 阅读http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

答案 1 :(得分:0)

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

最好给出你在hashmap中存储的类型,如:

HashMap<String, Integer> hashmapofcount = new HashMap<String, Integer>();

然后你可以用这种循环轻松遍历它:

   for (Map.Entry<String,Integer> entry : hashmapofcount.entrySet()) {
        final String description = entry.getKey();
        final Integer value = entry.getValue();
    }

PS:而且你不需要很多boxing整数和双精度,这会让你的代码看起来很糟糕。另外你要添加两个整数和双精度unitPrice和costValue,我想你可能想用unitPrice+" "+costValue(?)

连接它们