Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

How Do I Cast Object To Int ?

  Asked By: Cesar    Date: Mar 01    Category: Java    Views: 2625
  

I have an Object t which holds an ID (of type String) and a Number
(of type int)

public int compareTo(Object t){
int temp = ((Integer)t).intValue();
return temp;
}


my compareTo() method just needs to return the int value of the
object so that i could do something like this for instance.

if (t1.compareTo(t2) >= 0) {
..
..
..
}

But it doesnt work, any help would be great.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Vidhya Iyer     Answered On: Mar 01

If your object  has two properties, a String property and a primitive
int property than you need a way to retrieve those properties. You
need getters. Normally, when you write the class file for the object
class you would write the getter method  there. Write now you are
trying to cast  the entire object as an Integer which will produce
undesired results,

 
Answer #2    Answered By: Alvin Nguyen     Answered On: Mar 01

I dont know what you are trying to compare. If t1 and t2 are
instances of type  Double or Integer..etc, you can just use that
specific Wrapper class's compareTo() method.
If you really want to cast  that Object which is of type String to an
int, this is how:

public int  compareTo (Object t)
{
int temp  = Integer.parseInt ( String )t );
return temp;
}

 
Answer #3    Answered By: Jackson Bouchard     Answered On: Mar 01

I tried using your code but i'm still getting a
ClassCastException

is this because my object  is holding 2 different properites int  and
String ?

 
Answer #4    Answered By: Isaac Williams     Answered On: Mar 01

even I wonder what exactly r you trying to do.
Your object  t is not a string  object and thus you
cannot do Integer.parseInt(String t). What I would
like to ask is why do you want int  value for the
object as a whole. You have an int property, put a
getter to get that or if you need an id for the object
as a whole get the object ID.

 
Answer #5    Answered By: Luki Fischer     Answered On: Mar 01

I am trying to ignore exactly what it is your doing, but however offer
the following options.

If you are expecting an Integer object, then why not make the
parameter an integer?

public int  compareTo(Integer t){
int temp  = t.intValue();
return temp;
}

otherwise, check for the instance  so you won't get a
ClassCastException. Here's a couple of examples (I prefere the first
one):

public int compareTo(Object t){
int temp = DEFAULT_VALUE;
if (t instanceof Integer) {
temp = ((Integer)t).intValue();
}
return temp;
}

public int compareTo(Object t){
int temp = -1;

try {
temp = ((Integer)t).intValue();
} catch (Exception e) {
// ignore
temp = DEFAULT_VALUE;
}

return temp;
}

 
Answer #6    Answered By: Dennis Hayes     Answered On: Mar 01

I've been sitting here watching the posts and I have decided that I need
to speak up:

All of the examples have been incorrectly demonstrating what the
compareTo() method  is supposed return: -1, 0 or 1 depending on whether
the value passed in is less than, equal to, or greater than the object
which has the compareTo() method. The compareTo() should not return  the
int value of the object  being passed in - that is not its purpose.

The original posters problem is that the Object t which is passed to the
method should be cast  to his object type. Since the original poster did
not say what his object type  is, I am just going to call it MyObject:

public class MyObject{

public int  getNumber(){
return number;
}

public string  getId(){
return id;
}

public int compareTo(Object o){
int n = ((MyObject)o).getNumber();
if(n < number){
return -1;
} else if(n == number){
return 0;
} else {
return 1;
}
}

private int number;
private String id;

}

You can see that MyObject implements the compareTo() method by casting
the incoming object to a MyObject type if possible and then getting the
number from the incoming object and comparing that to its own number.
This is the correct usage of the compareTo() method.

A ClassCastException is the normal result when you try to compare two
objects which are not of the same type, unless the comparator knows that
it should expect different object types, usually be requiring some sort
of common interface instead.

 
Answer #7    Answered By: Canan Kaya     Answered On: Mar 01

this is EXACTLY what i was after!! i have tried it out & it's working the way i
want it too!
i guess i shouldve explained my self a little more and the very begining and my
question may have got answered a lot quicker with less confusion (well at least
i know for next time now!)

 
Didn't find what you were looking for? Find more on How Do I Cast Object To Int ? Or get search suggestion and latest updates.




Tagged: