File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do I convert an object into a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I convert an object into a string?" Watch "How do I convert an object into a string?" New topic
Author

How do I convert an object into a string?

andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
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

Joined: Jul 29, 2002
Posts: 31
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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135

You can override toString(), if desired.
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306

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

Joined: Jul 31, 2003
Posts: 263
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,


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
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

Joined: Nov 10, 2005
Posts: 7
bumping because my question may get lost in the midst.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

As of right now I get an error when using the bufferedreader code.
What error?
andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
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

Joined: Jul 31, 2003
Posts: 263
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

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I convert an object into a string?