如何将对象转换为int

时间:2022-07-04 19:37:04

I want to convert an object to int type.... eg: Object obj=........; int count = Integer.parseInt((String) obj);

我想将一个对象转换为int类型....例如:Object obj = ........; int count = Integer.parseInt((String)obj);

when i use above code ai got cast exception.

当我使用上面的代码ai得到强制转换异常。

Anyone know how to cast object to int...?

任何人都知道如何将对象转换为int ...?

2 个解决方案

#1


0  

Use obj.hashCode(), to get an int that represents the object. But what is your purpose? The code you posted does not work - you try to cast the object to a string rather than calling toString(), but even then, unless the toString representation of the object is itself an integer, calling Integer.parseInt on it will throw an exception.

使用obj.hashCode()来获取表示对象的int。但是你的目的是什么?您发布的代码不起作用 - 您尝试将对象转换为字符串而不是调用toString(),但即便如此,除非对象的toString表示本身是一个整数,否则调用Integer.parseInt会抛出异常。

So what are you aiming at?

那么你的目标是什么?

#2


0  

If tableres is a Hashtable, the values are prolly Integer, not int. In that case, try:

如果tableres是Hashtable,则值大概为Integer,而不是int。在这种情况下,请尝试:

int i = ((Integer) tableres.get ("mCount")).intValue();

int i =((Integer)tableres.get(“mCount”))。intValue();

Good luck, - M.S.

祝你好运, - M.S.

#1


0  

Use obj.hashCode(), to get an int that represents the object. But what is your purpose? The code you posted does not work - you try to cast the object to a string rather than calling toString(), but even then, unless the toString representation of the object is itself an integer, calling Integer.parseInt on it will throw an exception.

使用obj.hashCode()来获取表示对象的int。但是你的目的是什么?您发布的代码不起作用 - 您尝试将对象转换为字符串而不是调用toString(),但即便如此,除非对象的toString表示本身是一个整数,否则调用Integer.parseInt会抛出异常。

So what are you aiming at?

那么你的目标是什么?

#2


0  

If tableres is a Hashtable, the values are prolly Integer, not int. In that case, try:

如果tableres是Hashtable,则值大概为Integer,而不是int。在这种情况下,请尝试:

int i = ((Integer) tableres.get ("mCount")).intValue();

int i =((Integer)tableres.get(“mCount”))。intValue();

Good luck, - M.S.

祝你好运, - M.S.