2014-09-06 28 views
2

有誰知道如何解決此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); 
} 

} 
+2

請幫我們一個忙,並從代碼中刪除所有干擾註釋。我們越容易閱讀和理解你的代碼,就越容易幫助你。此外,請註明明顯的評論,可能是應該在您的代碼中的僅**註釋,哪一行會導致您的問題。 – 2014-09-06 02:01:14

+0

你的意見使你的代碼不可讀... – 2014-09-06 02:06:41

+0

好吧,如果你看看文檔,HashMap $ Entry和Integer之間唯一的共同父對象是Object。 – 2014-09-06 02:07:36

回答

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整數和雙打,這使得你的鱈魚呃看起來不可怕。另外一件事是你增加了兩個整數和雙倍單位價格和costValue,我想你可能想要連接它們通過使用unitPrice+" "+costValue(?)

+0

我想我只需要學習更多關於hashmap ......謝謝。 – NotTheOne 2014-09-06 03:28:12

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

howmanytimestheitemappears[i]是類型的HashMap $入境。拿到鑰匙您需要致電howmanytimestheitemappears[i].getKey() 閱讀http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

+0

如果我將它改爲'howmanytimestheitemappears [i] .getKey()'或甚至'howmanytimestheitemappears [i] .getValue()',那麼它會給我一個錯誤,說明「方法getKey()未定義爲Object類型」。我該如何解決?對不起,我是這個新生,但希望能夠快速學習。 – NotTheOne 2014-09-06 02:24:12

+0

更改'對象howmanytimestheitemappears []''Map.Entry howmanytimestheitemappears []' – 2014-09-06 02:25:20

+0

如果我這樣做了,它告訴我Map無法解析爲一個類型。 – NotTheOne 2014-09-06 02:30:05