有誰知道如何解決此ClassCastException錯誤?我得到: 「線程中的異常」主「java.lang.ClassCastException:java.util.HashMap $條目不能轉換爲java.lang.Integer?HashMap上的ClassCastException爲整數
我的問題是,我認爲我是在調用整數在該位置,但顯然不是?這種分配是由於在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);
}
}
請幫我們一個忙,並從代碼中刪除所有干擾註釋。我們越容易閱讀和理解你的代碼,就越容易幫助你。此外,請註明明顯的評論,可能是應該在您的代碼中的僅**註釋,哪一行會導致您的問題。 – 2014-09-06 02:01:14
你的意見使你的代碼不可讀... – 2014-09-06 02:06:41
好吧,如果你看看文檔,HashMap $ Entry和Integer之間唯一的共同父對象是Object。 – 2014-09-06 02:07:36