I have a text file , I am reading that text file and fetching some value from there, I am getting those value as string type. I have class that expects value of its type, this means I have to pass those strings which I am fetching from the text file by converting to my or better to say user defined class type. I am not able to convert the string to user defined class type. I am developing my code in core java. please suggest me how to convert this.
-
What do you mean by "convert" a string to class? You need to provide some more context, example of your class, and better definition of what you are talking about. You also don't seem to have done much research into it yourself.Ashkan Aryan– Ashkan Aryan2011-08-25 09:50:33 +00:00Commented Aug 25, 2011 at 9:50
-
1What do you mean by converting String to class type ?Swagatika– Swagatika2011-08-25 09:51:03 +00:00Commented Aug 25, 2011 at 9:51
-
There are some existing solution for various formats (xml, json, csv). Please tell us, if the text file has a "standard" structure. (or show an example)Andreas Dolk– Andreas Dolk2011-08-25 09:51:30 +00:00Commented Aug 25, 2011 at 9:51
-
-1 for a very lousy and imprecise question, which in its current form is impossible to answer with other than guesses.Péter Török– Péter Török2011-08-25 09:56:13 +00:00Commented Aug 25, 2011 at 9:56
Add a comment
|
3 Answers
Sounds like you're describing a form of parsing. You probably want to look into so called parser generators.
A parser translates text to objects (which seems to be what you need) and a parser generator takes as input a description of how to construct the objects from strings, and generates a parser accordingly.
A few examples I've used my self:
Comments
so you have a string and want to make an object of it?
for example you read the string into input_String; can't you just give that string to the constructor of the class?
String input_String = textScanner.readLine() //or something like that
myObject = ClassToInstantiate(input_String);
or am I not getting the question right?