I'm continuing to work on the Assignment OOP-2 and I've come across another delima. I'm trying to access the value of a hashmap by passing a
string value. I've since found that it doesn't like that. It obviously requires an argument of type object. Why doesn't automatic promotion work in this case. Then I'm trying to return the value from the hashmap as an int. This again does not work and it doesn't allow me to cast down from an object to an int or Integer.
Here is a portion of the code that compiles but gives me the following runtime exception: java.lang.ClassCastException: java.lang.String
StringTokenizer st = new StringTokenizer(args[0], "-");
int howMany = st.countTokens();
if (howMany == 2) {
String first = st.nextToken();
String second = st.nextToken();
String third = args[1];
Object one = first;
Object two = second;
Object three = third;
Integer temp = (Integer)map.get(one);
int num1 = temp.intValue();
Integer temp2 = (Integer)map.get(two);
int num2 = temp2.intValue();
int num3 = num2+num1;
Integer temp3 = (Integer)map.get(three);
int num4 = temp3.intValue();
Any help is appreciated
Thanks,
Jeremy