1

I have a hashmap of words, and the frequency I want to use the frequency as an integer

     for (Map.Entry me : hm.entrySet()) {
        int freq = me.getValue();
        //do something with int
     }

This keeps getting different errors: Cannot convert from Object to int, The method parseInt(boolean) in the type PApplet is not applicable for the arguments (object)

How do I workaround this?

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Tom Bar-Gal
  • 209
  • 2
  • 4
  • 13
  • 1
    Can you please post a [mcve] instead of a disconnected snippet? – Kevin Workman Apr 27 '18 at 16:14
  • מה קורה תום ? פונה אלייך בשפה שלנו. החברה בסטאק אוברפלו מאוד קשוחים. אם אתה מפרסם שאלה יכנסו בך חזק אם לא תקפיד על ניסוח תקין לשאלות שלך. בינתיים רק אני הצבעתי עבור השאלה שלך, אני חושב שהיא לגיטימית אבל יש פה הרבה שיתעצבנו על שאלה כזו. עשה טובה - פרסם עוד מהקוד שלך כדי שנבין מאיפה בדיוק מגיעה השגיאה שלך. שמור על עצמך סחבי, יש לך גב פה :) – Rann Lifshitz Apr 27 '18 at 18:05

2 Answers2

3

You should avoid using raw type, if you are using jdk7+, try create map this way:

Map<String, Integer> map = new HashMap<>();

for (Map.Entry<String, Integer> me : hm.entrySet()) {
     int freq = me.getValue();
     //do something with int
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
1

Consider this an Anti-Answer, but this does have its uses in a pinch. The answer by user6690200 is correct IMHO.

But what can you do if you can not control the creation of the Map, and are given an instance of the raw map ?

In this case, the quickest solution would be casting and surrounding the problematic casting with proper exception try-catch net:

for (Map.Entry me : hm.entrySet()) {
    try {
       int freq = (Integer)me.getValue();
       //do something with int
    } catch (ClassCastException e) {
       // handle exception resposibly!
    }
 }
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42