开发者

How to convert string type variable to user defined class type variable

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 con开发者_开发问答vert the string to user defined class type. I am developing my code in core java. please suggest me how to convert this.


Do you want something like this :

Class A {
   A(String str)
   {
      this.word = str;
   }
   private String word;
}

And then pass the objects of A, which will have the string values in the field.


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:

  • JavaCup (which is compatible with JFLex)
  • ANTLR


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜