-
November 3rd, 2004, 04:32 AM
#1
Member
Java casting problem
public int compareToObj(Object obj){
String temp = null;
try{
temp = (String)(obj);
}catch(Exception e){
throw new ClassCastException();
}
return compareTo(temp);
}
for some reson this will not cast to a String, and i know i am sending it a String. Any body have any ideas?
-
November 3rd, 2004, 04:40 AM
#2
-
November 3rd, 2004, 09:39 AM
#3
For future reference: there are execellent Java forums at http://forum.java.sun.com
That aside, your code (modified slightly to this) compiles and executes at my computer.
java -version gives me this:
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)
Code:
class Test
{
private int idx;
public static void main(String[] args)
{
System.out.println("Start");
Test test = new Test();
test.initialize();
System.out.println("Exit");
}
private void initialize()
{
idx = compareToObj("test");
}
public int compareToObj(Object obj)
{
String temp = null;
try
{
temp = (String)(obj);
}
catch(Exception e)
{
throw new ClassCastException();
}
return 1;
}
}
I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.
-
November 3rd, 2004, 03:38 PM
#4
Member
Thanks for the help guys, Guus i still can not get it to work like that, but the obj.toString() seems to work great. Is there a reason for this? Do i need to update my java?
-
November 4th, 2004, 02:59 AM
#5
I think posting here works as well, there are many knowledgable members who know Java and many people learn from it being here as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|