File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Assignment OOP-2 - converting an object to an Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Assignment OOP-2 - converting an object to an Integer" Watch "Assignment OOP-2 - converting an object to an Integer" New topic
Author

Assignment OOP-2 - converting an object to an Integer

Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
I have created a HashMap object and extracted a value from it. I know this works because I can see the result of the HashMap.get() when I print. But for the life of me I cannot get the resulting value into an int variable, Integer object or BigInteger. The object type of what I put into the map is an Integer, but when I try to assign the results to an Integer, the compiler rejects it - see below in my code snippet:
Map textnum = new HashMap();
// then in a for loop I assign it a key value pair
Marilyn removed a couple lines of code
// now I can take it out using the string as the key
String s1 = args[0];
//and it prints just fine
System.out.println( "value1: " + textnum.get( S1 ) );
//but how do I assign the textnum.get() results to something I can do math with?
//the compiler rejects this:
Integer value = textnum.get( s1 );
incompatible types
found : java.lang.Object
required: java.lang.Integer
What am I missing?
[ October 07, 2002: Message edited by: Marilyn de Queiroz ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Integer value = textnum.get( s1 );
incompatible types
found : java.lang.Object
required: java.lang.Integer


Hashmap's get() returns an Object
You are trying to assign an Object to an Integer. It won't work. What other ways (besides assignment) can we use to "convert" one type into another type?
[ October 07, 2002: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
The only way I know is casting. But when I attempt to cast the result of the textnum.get() as a primitive such as int e.g.
int val1 = (int)( textnum.get( s1 ) );
I get "incontrovertible types" compile error.
And if I try to cast it as an Integer which is what I originally put into the HashMap value, it doesn't work either since you can't convert one unrelated object to another, that I know of.
Is there another way of converting? How come it doesn't return the same type of object I put into the hashmap in the first place?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You are correct. You can't cast an Object into a primitive.

One of the nice things about Java is that each Object remembers what it really is. So even if you call it an Object, it still knows that it is a String, or an Integer, or whatever it was originally. So you can cast it to whatever it was originally.

You cannot, however, cast a String to an Integer because the String never was an Integer, and it knows it wasn't.
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
The value that I put into the HashMap was an Integer object. If that is what you mean by remembering what it is. I tried this early on - check my initial posting:
Integer val1 = textnum.get( s1 );
but this does not compile because textnum.get() is type Object. No matter what type you put into the HashMap, it comes out a type Object. I can therefore assign it as an Object like so,
Object val1 = textnum.get( s1 );
but this doesn't help because I can't do math on an Object. I need it converted to something I can do math on.
The value of textnum.get() is simply a number like 5 or 7. You'd think that would be easy to pop into an int or Integer or BigInteger.
I'm baffled by this.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Marilyn de Queiroz:
You are correct. You can't cast an Object into a primitive.

...but maybe into something else.
How come it doesn't return the same type of object I put into the hashmap in the first place?

If I remember correctly, a hashmap takes objects and returns objects.
From the docs, check out the method put() for HashMap. It's constructor is
public Object put(Object key, Object value)
If we add an object that is not of type Object, there's an "automatic cast" since any object we put in there will be a subclass of Object. When you get your object back out, however, you usually need it in the form of a lower order object, so you have to do something yourself to get the type you want...
[ October 08, 2002: Message edited by: Pauline McNamara ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Nick Cabell:
I tried this early on - check my initial posting:

Integer val1 = textnum.get( s1 );


I see no casting in this statement.

but this does not compile because textnum.get() is type Object. No matter what type you put into the HashMap, it comes out a type Object.

True. It remembers what it is, but doesn't automatically convert back to what it really is (like Pauline said).

I suggest that you take the Object that you got and cast it to an Integer before you assign it to the Integer reference val1.
[ October 08, 2002: Message edited by: Marilyn de Queiroz ]
Samiul Karim
Greenhorn

Joined: Sep 30, 2002
Posts: 9
Just as some of the guys said, HashMap takes an Object in the put() and returns an Object in the get(). So, if u want an integer that u can use to perform arithmetic operations, u have to convert it into an "int" primitive type as the "Integer" type does not support arithmetic operations.
[ code removed -ds ]
This should do the trick.
[ October 09, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think we should give Nick a chance yet, so I removed the tell-all code examples.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Usually I prefer that you don't post code here. If you post great code, you will be robbing others of an education.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Dirk said: my truck has 319,000 miles on it and still gets 33+ miles/gallon.
What kind of truck? Does it have a heater? Are you off to the rodeo?
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
I got it now that the object coming back needs to be be cast (is that the right word here?) from an Object (that it is) to an Integer.
Then I just couldn't believe that you had to put the Integer back to an int just to multiply it. What sort of Integer object is it that can't even do multiplication? Oh well.
Thanks for the plentiful help. You guys are great. I'm on to throwing exceptions.
Nick
Samiul Karim
Greenhorn

Joined: Sep 30, 2002
Posts: 9
Hey Dirk, I am sorry for putting the code fragment. I completely forgot that the place is for learners.
--------
"What sort of Integer object is it that can't even do multiplication?"
Nick, the Integer class is a wrapper class for the int primitive. It is not meant for arithmetic operations but is a sort of utility class that enables u to use an int as an object. Same for the other wrapper classes also.
[ October 10, 2002: Message edited by: Marilyn de Queiroz ]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Nick Cabell:
What sort of Integer object is it that can't even do multiplication? Oh well.
Nick

Hee hee, the kind you can put in a hashmap
Have fun with exceptions.
[ October 10, 2002: Message edited by: Pauline McNamara ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment OOP-2 - converting an object to an Integer