Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

binary tree toString() method

807606Jun 4 2007 — edited Jun 4 2007
im writing an expression tree for my comp sci class and everything works except the part i coppied directly out of the book.

public String toString()
{
return toString(root, 0);
}
private String toString(Node<String> tree, int level)
{
String str="";
if(tree!=null)
{
str+= toString(tree.right, level+1);
for(int i=1; i<=level; i++)
str=str+"|";
str+=tree.value.toString()+"\n";
str+=toString(tree.left, level+1);
}
return str;
}
this is the to string method that should print the tree like this:
| | x
| x |
| | x
x | |
| | x
| x |
| | x
but the computer doesnt like it when i compile it.

LinkedBSTPT.java:90: toString() in LinkedBSTPT cannot override toString() in
java.lang.Object; attempting to use incompatible return type
found : String
required: java.lang.String
public String toString()
^
LinkedBSTPT.java:96: incompatible types
found : java.lang.String
required: String
String str="";
^
LinkedBSTPT.java:99: operator + cannot be applied to String,String
str+= toString(tree.right, level+1);
^
LinkedBSTPT.java:101: incompatible types
found : java.lang.String
required: String
str=str+"|";
^
LinkedBSTPT.java:102: incompatible types
found : String
required: java.lang.String
str+=tree.value.toString()+"\n";
^
LinkedBSTPT.java:103: operator + cannot be applied to String,String
str+=toString(tree.left, level+1);
^
these are the errors it give me. some one please explain to me why the computer doesnt like this chunk of code!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 2 2007
Added on Jun 4 2007
6 comments
1,884 views