aspose file tools*
The moose likes Beginning Java and the fly likes string to object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string to object" Watch "string to object" New topic
Author

string to object

tom brownlee
Greenhorn

Joined: Aug 14, 2001
Posts: 28
how do i convert (cast) a String to an Object,
i.e
input a String from user and cast it to an Object to be manipulated
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can you give an example of such a string which would be input by a user. Also, what kind (Class) of object do you want manufactured?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
you can do this:
String s="some string";
Object obj=s;//this is correct and works!

The problem is, obj can't be manipulated, and a s either... Strings are immutable. If you want to manipulate the content of a string try:
String s="some string";
StringBuffer buf=new StringBuffer(s);
buf.replace(5,11,"chars");
buf.insert(10," here");
String result=buf.toString();//the content of result at this line is "some chars here";

hope it helps!!We could give more details if you could give us more information!!!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
...and note that a String is an Object.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
tom brownlee
Greenhorn

Joined: Aug 14, 2001
Posts: 28
i have a text game im creating that has a 3x3 dimensioned space. i.e. 9 rooms in a 3x3 pattern. each room contains a jewel. i have a commandWords class that contains methods etc to read info from user. i also have a command class that determines first word and second word entries from the user i.e take jewel. my problem was in the game class that initiates the game i have a method 'take'.

here you can see that the second word is in the form of a colour (initial beginnings - it will get better) i.e. take Orange.
so here jewelColour equals the second word from the user (Orange). i need to cast it to the object Jewel (jewelInRoom) so i can manipulate it i.e. using if statements or while etc.
at the moment im printing to the console to test if im getting the cast right but i get the compile error due to the casting.


here i have in the 'Room' class two methods setContainedJewel and getContainedJewel in which i can change the jewel in each room when i 'take' the jewel.
does this help?
[ March 25, 2003: Message edited by: Dirk Schreckmann ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

If I understand you want to transform the string "Orange" into a Jewel object, is that correct?
If so I would suggest that you figure out a different way of solving this problem, because I don't know of any easy of making that transformation.
You don't need to have such a literal relationship between what the user enters on the keyboard and the objects that your code is working on.
Without knowing your complete objectives I would suggest you have an attibute on the Jewel class which is the color, this attribute could be a string. That would give you two strings to compare.


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Actually you can create a class from a string using the java.lang.reflect class. But you don't want to go there. (There are easyer ways of solving your problems.)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That's why I asked my original question. I thought there was going to be something like deserialization involved in this one
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
First off I think you should have the player say "take Jewel". Then you can create a Jewel and set its attributes like colour ("orange", size ("big"), placement ("table") from further answers or defaults.
So how do we get from "Jewel" to an instance of the Jewel class? Um, yes. HashTable with key "Jewel", value reference to a JewelFactory object, that has method: Goodie JewelFactory.make().
Suppose Jewel extends Goodie and JewelFactory extends GoodieFactory.

Sort of....
[ March 25, 2003: Message edited by: Barry Gaunt ]
[ March 25, 2003: Message edited by: Barry Gaunt ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Why are you putting a "factory" into a HashMap? That one confuses me.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Because I'm thinking of Swords, Cups, Bags, and stuff. So if user says "take Sword" he can look up the approriate factory object and make one.
I'm just brain dumping here...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In general a player would have some Goodie things. You would still have a problem deciding what type those things could be ( "Sword", "Jewel" ), so you would still have to use instanceof and/or use a String typeOf attribute.
Sort of... (that means "could be bullshit" )
tom brownlee
Greenhorn

Joined: Aug 14, 2001
Posts: 28
excellent guys..thanks for the feedback
 
Consider Paul's rocket mass heater.
 
subject: string to object
 
Similar Threads
(String) casts in SortNames
Casting rules
Inferring the correct type parameter
Assign String[][] to a Session Attribute
Casting Object[] to String[]