I'm trying to cast an Object to a type Procedure (a class I wrote) in order to access a method in the Procedure class. Problem is, Procedure is a subclass of Object, and it requires a run-time check to verify that the object that I'm trying to cast as a Procedure object is actually a Procedure object in the first place. How do I go about implementing this run-time check?

The code is an implementation of the method compareTo() in the interface Comparable. It requires that an Object be passed, so passing an actual Procedure instance is out of the question. I need to cast it back into a Procedure object from an Object.

Ok, I did a little research and found that I can use the instanceof operator to check if the variable that is passed is an instance of the Procedures class. Where do I go from there? Here's the block of code that I'm working with:

/** 
     * Compare this procedure to the one passed in as an argument. 
     * Procedures are compared based on their names only - case is 
     * ignored! 
     * 
     * @param other The Procedure object to compare this to. 
     *
     * @return 0 if the names of the two procedures are equal. 
     *         a value < 0 if the name of this procedure is less than the name
     *         of the procedure passed in. 
     *         a value > 0 if the name of this procedure is greater than the 
     *         name of the procedure passed in. 
     */

    public int compareTo&#40; Object other &#41; &#123;
	int retint;
	if&#40; other instanceof Procedure &#41; &#123;
	    String newName = new String&#40; &#40;Procedure&#41;other.getName&#40;&#41; &#41;;
	    retint = name.compareToIgnoreCase&#40;other.getName&#40;&#41;&#41;;
	&#125;
	return retint;
    &#125;

The Procedure class represents a medical procedure. I'm writing it as part of a doctor's office simulation for my CS class project. Each procedure , when created, is given a name, a cost, a duration, and two boolean values that tell it whether or not it is covered by HMO Insurance and/or Private Insurance. The compareTo method in Procedure is supposed to compare the Procedure that it is called on with another Procedure that is passed in as an Object. The two Procedures are compared by name only, and case is ignored. It is supposed to return an int value that is either negative (if the first String is less than the second), positive (if the second String is less than the first), or zero (if both Strings are equal).

As far as I can tell, the String method compareToIgnoreCase will do exactly what I need done (ignoring case, return a negative, positive, or zero depending on the two Strings' relationship), once I can get the name of the 'other' Procedure object.

name is the String within this instance of Procedure that I am comparing with the String name of 'other,' which is another Procedure object.


getName() is the method that I am trying to call from within the Procedure class to get it to return the name of the Procedure 'other'. The class implements the interface Comparable, and this is as far as I've gotten. Its a bit of a hodge-podge as it is now, but if anyone could steer me in the right direction I'd appreciate it.

Well, I got it... I was missing a damn set of parentheses... :|

The functional return statement looks like this:

int retint = 0;
if&#40; other instanceof Procedure &#41; &#123;
    retint = name.compareToIgnoreCase&#40; &#40;&#40;Procedure&#41;other&#41;.getName&#40;&#41; &#41;;
&#125;
return retint

All thanks to that extra set of parentheses around ((Procedure)other), gosh darnit. Well, I'm glad I got that figured out.

Stuff like that always happens to me! I've learned the best way to get past a place you are stuck is to just close it, walk away and come back the next day with a fresh mind.

This article has been dead for over six months. Start a new discussion instead.