Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I convert an object into a string?

 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just spent hours and hours implementing binary trees and linked list queues and stacks. Now the last part I have to implement a user interface to test these methods. The methods I have to test are inorder, poster, preorder traversals of a tree.

My problem is I have set up my tree with the insert method using an Object as the item. Now I have to insert the object as a string into my tree. But I keep getting an error that the string can not be inserted as the tree is expect an object.

Can any one point me in the right direction?
 
Wes Hughes
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're gonna need to explain it a little better or post some code cause from my understanding you have written a method which expects an Object as argument and you are passing in a String (which extends Object), therefore you shouldn't have any problem.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy and paste the exact error message and a piece of the relevant code in which the error occurs - that would make it easier to help you.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can override toString(), if desired.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But I keep getting an error that the string can not be inserted as the tree is expect an object.

A string is subclass of java.lang.Object. Wherever an Object is expected a String is welcomed.

Either you have a custom class called Object like org.omg.CORBA.Object or something you observed is not right.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you have set your insert method up correctly, but your tree implementation is backwards. You want you tree implementation to work with Objects as well, and not Strings. You can put a String into a tree expecting objects, but you cannot put an arbritary object into a tree expecting Strings unless the object is a String, or a subclass of String.

Post your code and we'll give you some advice on what sounds like a homework problem.

Cheers,
 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

How did you know this was a homework assignment? Must be the basic nature of the question. Your advice of adding the toString did help some but ofcourse I come across another error. The toString made sence shortly after asking this question.

The new problem now is in the user interface I am trying to put together, how do I put this. In previous programs I always had to input "int"'s as input and the only other kind was adding strings objects to a lexicon of words. In that case we used

"import.cs1.keyboard;
String FID;
FID = Keyboard.readString();

to read the inputted string that we are adding. The below code is the method I used to read the inputted int's. What would I use to read the similar input but has string as inputs.

BufferedReader keyBoard = new BufferedReader(new InputStreamReader(System.in));
int length = Integer.parseInt(keyBoard.readLine());

As of right now I get an error when using the bufferedreader code.

So what code could I use to read the input string object so it could be inputted to my binary tree.

Thanks, you all seem very helpful and I would like to thank you in advance.
 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bumping because my question may get lost in the midst.
 
Paul Clapham
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of right now I get an error when using the bufferedreader code.
What error?
 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok now I have new problems,

Below is the copied and pasted code from my file.

this is the error message I am getting. Seems like it is not letting me create the new Tree and use the insert method?


LinkedBinaryTree.java:28: cannot resolve symbol
symbol : method insert (java.lang.String)
location: class BTNode
newNode.insert(line)


public void main(String [] args) throws IOException{

BufferedReader ac = new BufferedReader(new InputStreamReader(System.in));

BTNode newNode = new BTNode();
String line = null;


System.out.println ("Please enter text (type DONE to quit):");

line = ac.readLine();
while ((line != ("DONE")) || line != ("done") )
{
newNode.insert(line);
line = ac.readLine();
}
}


this is the insert method I am using, does not have to be a search tree so no need to compare strings.

// inserts a an object into our tree.
public void insert(Object obj){

((BTNode) current).setElement(root);
if (isEmpty())
{ addRoot(obj); }
else

while( !isExternal(current) )
{

if( !hasLeft(current) ) //if no left node insert left
{
insertLeft(current, obj);
}
else
if( right(current) == null ) //if no right node insert right
{
insertRight(current, obj);
}
else
if( hasLeft(current) ) //if left node exsists set current node to the left
{
((BTNode ) current).setElement(left);
}
else
if ( hasRight(current) ) //if right node exsists set current node to the right
{
((BTNode ) current).setElement(right);
}
}
}
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where's your BT node Class. The error message is telling you that the BTNode class does not have an insert method that takes a single String argument.

Also, did you check to see what the toSTring method returned for your object? What you inserted in your list by using toString is probably not what you wanted.

Cheers,
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not your problem, but I can't help noticing you're making a
common Java beginner's mistake -- if you want to compare two strings for
equality, don't use operators == or !=, instead use the equals method:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic