File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string to object

 
tom brownlee
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and note that a String is an Object.
 
tom brownlee
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you putting a "factory" into a HashMap? That one confuses me.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excellent guys..thanks for the feedback
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic