Results 1 to 11 of 11
  1. #1
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default Type-Casting Object doesn't Give me the Methods I need

    Hello Friends

    I am slightly stuck and I would greatly appreciate any help.

    I have a Node class, and this class has several methods, one of which is [ getName() ] which returns a String that represents the name of the node.

    Here is my code:

    Java Code:
    if(isAtEntrance())
        	{
        		// A place to store the safe paths.
        		ArrayList<Stack<String>> paths = new ArrayList<Stack<String>>();
        		
        		// The variable g stands for, and is the Graph object < which in itself is a Hashtable.
        		Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>> g = getMap().getAdjacencyList();
        		
        		// Iterating through [g] to find safe paths.
        		Enumeration e = g.keys();
        		
        		while(e.hasMoreElements())
        		{
        			// A variable called [neighbours]. It represents the neighoubrs of the current node in the loop
        			ArrayList<Node<Adjustment>> neighbours = g.get((Node<Adjustment>)e.nextElement());
        			
        			// The Path [p] represented as a Stack. This will be inserted into our safe [paths] ArrayList later on.
        			Stack<String> p = new Stack<String>();
        			
        			// Iterating through the neighbours of the current node, and foreach building a path [p] from them. 
        			for(Node n : neighbours)
        			{
        				p.push((Node<Adjustment>)e.nextElement().getName());
        				p.push(n.getName());
        			}
        		}
        		
        	}
    A couple of things are happening here (I hope you can see), but the gist of it is that I am iterating through a Hashtable.

    e.nextElement() is an object. I am type casting it to a Node<Adjustment> but it isn't working. How do I know it isn't working ? .. It isn't giving me my getName() method.

    What am I doing wrong ? I would greatly appreciate any help.

    Thank You.
    >> What can be asserted without proof can be dismissed without proof. <<

  2. #2
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    did you try consulting the API ?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    The first thing I see wrong with that code is multiple calls to e.nextElement() in a while(e.hasMoreElements()) loop. That's almost guaranteed to blow up.

    db

  4. #4
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default

    Not really. I'm not sure what I'd be checking in the API.

    I did google Type Casting in Java, cause I believe that's where the issue lies. am I wrong ?
    >> What can be asserted without proof can be dismissed without proof. <<

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    Try debugging your code by assigning the value returned by nextElement to a variable and printing out that variable to see what is being returned. That will require adding a few more statements.

  6. #6
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default

    Thank You DarrylBruke

    I did not realise that I had to only have it once within the loop. I have changed my code to the following:

    Java Code:
    if(isAtEntrance())
        	{
        		// A place to store the safe paths.
        		ArrayList<Stack<String>> paths = new ArrayList<Stack<String>>();
        		
        		// The variable g stands for, and is the Graph object < which in itself is a Hashtable.
        		Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>> g = getMap().getAdjacencyList();
        		
        		// Iterating through [g] to find safe paths.
        		Enumeration e = g.keys();
        		
        		while(e.hasMoreElements())
        		{
        			// e.nextElement can only be used once within the while loop. So I'm assigning it to a variable that can be used many times.
        			Node<Adjustment> x = (Node<Adjustment>)e.nextElement();
        			
        			// A variable called [neighbours]. It represents the neighoubrs of the current node in the loop
        			ArrayList<Node<Adjustment>> neighbours = g.get(x);
        			
        			// The Path [p] represented as a Stack. This will be inserted into our safe [paths] ArrayList later on.
        			Stack<String> p = new Stack<String>();
        			
        			// Iterating through the neighbours of the current node, and foreach building a path [p] from them. 
        			for(Node n : neighbours)
        			{
        				p.push(x.getName());
        				p.push(n.getName());
        			}
        		}
        		
        	}
    Seems to be OK now ! Eclipse does NOT give any errors !

    Can some Eclipse master please tell me what I'm suppose to do when I get a warning such as:

    Node is a raw type. References to generic type Node<NodeInfo> should be parameterized
    or

    Enumeration is a raw type. References to generic type Enumeration<E> should be parameterized
    Do I ignore these cause they are Warnings and not Errors ? Or are they serious and I need to do something about them ? .. if the latter, I have no idea what to do !
    >> What can be asserted without proof can be dismissed without proof. <<

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    You need to use generics with those classes the same as you have used it in other places in your code.
    For example what type does g.keys() return?

  8. #8
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default

    Hello Norm

    Still slightly confused ! g.keys() returns Enumeration<Node<Adjustment>>

    Or at least that's what Eclipse says when I hover over the word keys(). !!

    Sincere apologies if my dumbness in Java is causing you guys stress and unhappy feelings.
    >> What can be asserted without proof can be dismissed without proof. <<

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    g.keys() returns Enumeration<Node<Adjustment>>
    Look at what is to the left of "= g.keys();" and then look at the error message.

    You forgot to post the lines that go with the error messages.

    How did you code this code using so many generics and then have this problem???
    Last edited by Norm; 06-26-2011 at 12:32 AM.

  10. #10
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default

    Here is the code:

    Java Code:
    Enumeration e = g.keys();
    And here's its error:

    Enumeration is a raw type. References to generic type Enumeration<E> should be parameterized
    I think I got what you mean now though. It turns out that the best practice is to make lucid the type of all variables. So I went ahead and did this:

    Java Code:
    Enumeration<Node<Adjustment>> e = g.keys();
    And violla ! the warning is gone !

    Thank You Ever So Much.
    Last edited by Ciwan; 06-26-2011 at 12:48 AM.
    >> What can be asserted without proof can be dismissed without proof. <<

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

Similar Threads

  1. Type casting
    By kiros88 in forum New To Java
    Replies: 17
    Last Post: 09-02-2010, 01:58 PM
  2. Type Casting
    By Shaheen Mohamed in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 07:56 PM
  3. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 10:52 AM
  4. help with type casting.
    By ramsrocker in forum Java Applets
    Replies: 15
    Last Post: 02-26-2009, 11:28 PM
  5. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •