Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Simple Question....I hope

Thread view: 
Lawrence - 05 Jan 2007 14:04 GMT
How do I convert a variable of datatype object to a string?

For example I have a variable item with datatype object Item, and I
want to compare it to an input by the user which is a string.

e.g. if(item == string)

How do I convert the item to a string so I dont get incompatible
datatypes?
Paul Hamaker - 05 Jan 2007 14:23 GMT
You could compare the Item's string representation (provided it has
one) like this :
if ( item.toString().equals( string ) )  ...
--
http://javalessons.com  Paul Hamaker, SEMM
Teaching ICT since 1987
Lawrence - 05 Jan 2007 14:45 GMT
Ok thanks for that, it compiled but didnt work as expected.

So I tried printing it out using the item.toString()

And it outputted: Item@4b0bbb

So this makes me think that it doesnt have a string representation.

Any help on how to do that?

Cheers

> You could compare the Item's string representation (provided it has
> one) like this :
> if ( item.toString().equals( string ) )  ...
> --
> http://javalessons.com  Paul Hamaker, SEMM
> Teaching ICT since 1987
Paul Hamaker - 05 Jan 2007 15:05 GMT
If you can change Item's source code, you can add this method :
public String toString()
{
 //make string s based on Item's fields
 return s;
}
--
http://javalessons.com  Paul Hamaker, SEMM
Teaching ICT since 1987
Lawrence - 05 Jan 2007 15:28 GMT
ok thanks for that, works now to an extent.
Just for reference could I change a string to an Item?

> If you can change Item's source code, you can add this method :
> public String toString()
[quoted text clipped - 5 lines]
> http://javalessons.com  Paul Hamaker, SEMM
> Teaching ICT since 1987
Fred Kleinschmidt - 05 Jan 2007 19:29 GMT
Don't top-post, and don't remove the reference to the original question.

> ok thanks for that, works now to an extent.
> Just for reference could I change a string to an Item?

Just create a constructor that takes a string as its argument.
It's up to you to determine what it means to convert a string
into an Item, and that is what this constructor should do.

Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

steve - 05 Jan 2007 22:44 GMT
> ok thanks for that, works now to an extent.
> Just for reference could I change a string to an Item?
[quoted text clipped - 8 lines]
>> http://javalessons.com  Paul Hamaker, SEMM
>> Teaching ICT since 1987

you need to get into the mind set of using the correct terms.

consider Using the word "Object"

Steve
Lew - 06 Jan 2007 03:00 GMT
> So I tried printing it out using the item.toString()
>
> And it outputted: Item@4b0bbb
>
> So this makes me think that it doesnt have a string representation.

It did, indeed, have a string representation: "Item@4b0bbb".

- Lew
Lew - 06 Jan 2007 02:59 GMT
> You could compare the Item's string representation (provided it has one)

Don't all objects have a String representation, at least the one inherited
from Object.toString()?

<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html>
"The toString method for class Object returns a string consisting of the name
of the class of which the object is an instance, the at-sign character `@',
and the unsigned hexadecimal representation of the hash code of the object."

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.