Results 1 to 4 of 4
  1. #1
    sardare is offline Member
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Default runtime type conversion

    Hi,

    I have a mapping from variables to types. I am reading in those variables from a file as string and I need to convert them to their type dynamically. Since there are 100 or so varialbles, I don't want to code type conversion of each of them.

    e.g. variable to type mapping:
    public class Var {
    public static Var v1 = new Var("double");
    public static Var v2 = new Var("int");
    public static Var v2 = new Var("String");

    public Var(String type) {
    _type = type;
    }

    public String getType() {
    return _type;
    }
    }

    Now I read in the variables from a file as strings. How do I dynamically convert them to their corresponding types?

    thanks for your help/

    cheers,

    Samit

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    why dont you use a switch stmt?

    Its just a suggestion i am not sure how good it will work.
    To finish sooner, take your own time....
    Nivedithaaaa

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Niveditha View Post
    why dont you use a switch stmt?
    Switch statements are poor OO, you are supposed to use polymorphism.

    Of course, I backslide and use switch statements when needed.

Similar Threads

  1. Doc to Pdf conversion
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2009, 01:27 PM
  2. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM
  3. Conversion from wav to vox
    By bozovilla in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2008, 06:54 AM
  4. binary conversion
    By thamizhisai in forum New To Java
    Replies: 3
    Last Post: 04-29-2008, 12:55 PM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •