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!!!
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 ]
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.
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
posted
0
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
posted
0
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 ]
Why are you putting a "factory" into a HashMap? That one confuses me.
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
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
posted
0
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" )