Results 1 to 5 of 5

Thread: Java casting problem

  1. #1

    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?

  2. #2
    Banned
    Join Date
    Sep 2004
    Posts
    305
    obj.toString();

  3. #3
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    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.

  4. #4
    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?

  5. #5
    Banned
    Join Date
    Sep 2004
    Posts
    305
    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
  •