Storing strings in binary search tree

On the below code I get: ClassCastException : java.lang.String. I am trying to build a binary search tree from a text file one word at a time. when I call this method below the first time it works fine, but on the second call it gets the error at the second if statement:

http://rafb.net/paste/results/ieJkKd64.html

This is the code I use to call on the above function:

http://rafb.net/paste/results/j5MPDn59.html

Also, this interface is used by the insertBST function:

http://rafb.net/paste/results/0VQjVm42.html

[551 byte] By [EzMea] at [2007-11-26 12:32:30]
# 1
Most people will not go to those unknown sites. Post a small working program that is an example of the problem here. Format your code between code tags, as explained in the Formatting tips link above the typing area.
ChuckBinga at 2007-7-7 15:45:32 > top of Java-index,Java Essentials,New To Java...
# 2

> Most people will not go to those unknown sites. Post

> a small working program that is an example of the

> problem here.

>

> Format your code between code tags, as explained in

> the Formatting tips link above the typing area.

On the below code I get: ClassCastException : java.lang.String. I am trying to build a binary search tree from a text file one word at a time. when I call this method below the first time it works fine, but on the second call it gets the error at the second if statement:

public void insertBST(Object o) {

TreeNode p, q;

TreeNode r = new TreeNode(o);

if (root == null)

root = r;

else {

p = root;

q = root;

while (q != null) {

p = q;

System.out.println(r.getInfo());

if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)

q = p.getLeft();

else

q = p.getRight();

}

if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)

setLeftChild(p, r);

else

setRightChild(p, r);

}

}

This is the code I use to call on the above function:

public void readFromFile() {

//int i = 1;

while (fileScan.hasNext()) {

String buf = fileScan.next();

hash(buf);

if (table[tableIndex] == tableIndex) {

System.out.println("Same Omitted Word");

}

else{

obt.insertBST(buf);

System.out.println("Not an Omitted Word");

}

}

}

public int hash(String s) {

sum = 0;

for(int i = 0; i< s.length(); i++)

sum += (int)s.charAt(i);

tableIndex = sum%TABLESIZE;

return tableIndex;

}

-Also, this interface is used by the insertBST function:

public interface TreeComparable {

public int compareTo(Object o);

public void operate(Object o);

public void visit();

EzMea at 2007-7-7 15:45:32 > top of Java-index,Java Essentials,New To Java...
# 3

A ClassCastException is thrown when you try and cast an object to a subclass of that is not an instance like shown in the example below:

Integer i = new Integer(5);

String s = (String)i;

String is not a subclass of Integer (ie: String does not extend the Integer class)

I assume getInfo returns a String, and then you are trying to cast that to a TreeComparable. Since String does not extend TreeComparable, it throws that exception.

Also, does your class implement TreeComparable? And why exactly do you have to cast it to a TreeComparable anyways? That really makes no sense

Minkoa at 2007-7-7 15:45:32 > top of Java-index,Java Essentials,New To Java...
# 4

> A ClassCastException is thrown when you try and cast

> an object to a subclass of that is not an instance

> like shown in the example below:

>

> Integer i = new Integer(5);

> String s = (String)i;

>

> String is not a subclass of Integer (ie: String does

> not extend the Integer class)

>

> I assume getInfo returns a String, and then

> you are trying to cast that to a TreeComparable.

> Since String does not extend TreeComparable, it

> throws that exception.

>

> Also, does your class implement TreeComparable? And

> why exactly do you have to cast it to a

> TreeComparable anyways? That really makes no sense

It is true that getInfo returns a String. I have tried declaring the class where insertBST resides as:

public class ObjectBinaryTree implements TreeComparable{

but I got the above b]ObjectBinaryTree class from the profesor, so I'm not sure if changing it is the best way.

If I do the above changes I get :

ObjectBinaryTree is not abstract and does not override abstract method visit() in TreeComparable

if I make ObjectBinaryTree abstract other classes in the same project say ObjectBinaryTree can not be instantiated

EzMea at 2007-7-7 15:45:32 > top of Java-index,Java Essentials,New To Java...