当前位置: 首页 > 知识库问答 >
问题:

Hashmap到Integer上的ClassCastException

贲绪
2023-03-14

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

我的问题是,我以为我调用的是那个位置的整数,但显然不是?这项作业两小时后到期,所以任何帮助都是非常感谢的。评论应该告诉你发生了什么。

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个答案

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

howmanytimestheite

张翰海
2023-03-14

首先,您的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:而且你不需要对整数和双精度进行大量的装箱,这使你的代码看起来有点糟糕。另一件事是添加两个整数并将 unitPrice 和 costValue 加倍,我认为您可能希望通过使用 unitPrice “ ” costValue(?

 类似资料:
  • 我已经编写了在android studio上显示可扩展列表视图的代码。以下是ExplandableListView的代码: 这是执行操作的方法: 它在函数定义中的“groupPosition”处显示警告: 当鼠标悬停在上面时,会显示

  • 问题内容: 有没有一种方法可以将包含json的字符串转换为HashMap,其中每个键都是json键,而值是json键的值?json没有嵌套值。我正在使用Gson库。 例如,给定JSON: 产生的HashMap: 谢谢 问题答案: 使用,按照该GSON常见问题: 没有铸造。没有不必要的对象创建。

  • 有没有一种方法可以将包含json的字符串转换为HashMap,其中每个键都是json-key,值是json-key的值?json没有嵌套值。我正在使用Gson库。 例如,给定JSON: 结果HashMap: 谢谢

  • 我有一个函数,在这个函数中,用户传递一个参数来选择应该处理矩阵的哪些列,如下所示: 我想让用户有一种方法来指定应该处理所有列,最后我更改了函数,使其成为默认行为: 为什么一个整数、一个整数向量和一个字符串之间的联合似乎不起作用?

  • 如果数字是整数,则返回true。 语法 (Syntax) 以下是语法。 (integer? number) 例子 (Example) 以下是整数测试函数的示例。 (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def x (integer?

  • 我想创建一个按键对HashMap进行排序的方法。HashMap的键可以是String或Long。这两种HashMap类型的处理是相似的。为了避免代码重复,我想创建一个方法来处理这两种情况。 该方法的代码: 是否可以创建一个具有单个参数的Java方法,该方法可以接收两个<code>HashMap的值