Creating primitive data types from contents of String

Discussion in 'Java' started by Jesper Sahner, Nov 13, 2004.

  1. Hi!

    Let's say that s is a String containing a name. How do I then
    (dynamically) create an integer with that specific name like:

    String s="abc";
    int abc;

    String s="def";
    int def;

    ....

    E.g. the String-names could be some names read from a file.

    Regards,
    Jesper
     
    Jesper Sahner, Nov 13, 2004
    #1
    1. Advertising

  2. Jesper Sahner <> scribbled the following:
    > Hi!


    > Let's say that s is a String containing a name. How do I then
    > (dynamically) create an integer with that specific name like:


    > String s="abc";
    > int abc;


    > String s="def";
    > int def;


    > ...


    > E.g. the String-names could be some names read from a file.


    You can't. Variable names, at least local ones (method scope) are an
    entirely compile-time concept and can't be manipulated at run-time.
    What are you really trying to accomplish with this?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "It's time, it's time, it's time to dump the slime!"
    - Dr. Dante
     
    Joona I Palaste, Nov 13, 2004
    #2
    1. Advertising

  3. Jesper Sahner

    Oscar kind Guest

    Jesper Sahner <> wrote:
    > Let's say that s is a String containing a name. How do I then
    > (dynamically) create an integer with that specific name like:
    >
    > String s="abc";
    > int abc;


    You don't. After all: how would you reference it?

    If you need to create labels for numbers/objects/... dynamically, use a
    Map. Also note that you cannot use primitive types like int, char,
    boolean, etc. Use Integer, Crahacer, Boolean, etc. instead.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Nov 13, 2004
    #3
  4. Jesper Sahner

    Alberto Guest

    I'm not sure why would you like to do that. However, if you are trying
    to dynamically create an object depending on input entered, you may
    want to read about "Reflection". Keep in mind this will only work on
    data objects, not on primitives.

    You won't be able to create a primitive, they are not objects. You
    could use wrapper classes (Objects) for those primitives (or you could
    write your own). If you still insit in storing and defining a variable
    from input, a good approach could be to use a data structure (create
    your own object) to do the abstraction for you.
     
    Alberto, Nov 13, 2004
    #4
  5. Hi again!

    Consider the following problem:
    You have some information stored in a data-file, and in addition you
    have a header-file with a description of the record-layout
    (variable-name, position, length, format, label etc.).

    The task then is to read from the data-file using the
    header-information. If e.g. the header contains a description of a
    variable 'var1' of type 'double' and another variable 'var2' of type
    'int' then the Java-code should declare these variables on basis of
    the header-information like:
    double var1;
    int var2;

    Then you could make some calculations involving var1 and var2, write
    the result to a new file etc. - very similar to a database-lookup.

    How would you do this?

    Regards,
    Jesper
     
    Jesper Sahner, Nov 14, 2004
    #5
  6. Jesper Sahner

    Sudsy Guest

    Jesper Sahner wrote:
    <snip>
    > The task then is to read from the data-file using the
    > header-information. If e.g. the header contains a description of a
    > variable 'var1' of type 'double' and another variable 'var2' of type
    > 'int' then the Java-code should declare these variables on basis of
    > the header-information like:
    > double var1;
    > int var2;
    >
    > Then you could make some calculations involving var1 and var2, write
    > the result to a new file etc. - very similar to a database-lookup.


    Again, your motive escapes me (and others, apparently).
    A variable is merely a convenient handle to either a primitive or an
    Object. The value or object being referenced has no need to know the
    name(s) (if any) which refer to it.
    BTW, I just LOVED the post which mentioned the scenario of foo( 3 )!
    Guess what? There's no variable name associated with the value!
    Please think carefully about what you're trying to achieve. It might
    turn out to be impossible, and for good reason.
    Perhaps if you describe what you're trying to accomplish? ...

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
     
    Sudsy, Nov 14, 2004
    #6
  7. Does the following page describe what you are trying to do?

    http://mindprod.com/jgloss/eval.html

    eval
    In many languages you can take a dynamically created String such as
    "6*(4+6^2)-cos(20)" and ask to have it evaluated, as if it were a
    miniature computer program. The function to do this often has a name
    such as eval. Java has no such function. What can you do? Here are
    four different approaches:
    ....

    --
    Regards,
    Casey
     
    Casey Hawthorne, Nov 14, 2004
    #7
  8. Jesper Sahner

    hiwa Guest

    (Jesper Sahner) wrote in message news:<>...

    I would write a simple text processing program which generate a
    relevant part -- variable declaration part -- of the target Java
    source code.

    As mentioned earlier, names in the source program are only relevant to
    compiler. In order to feed them to the compiler, we can not use
    nothing but ordinary source code. We have to make source text from
    your header etc.
     
    hiwa, Nov 14, 2004
    #8
  9. Hi!

    As I have described the task is somewhat similar to a database-lookup.

    It is true that a variable is just a reference to an object or a
    primitive type. However it is important that these variables have
    names corresponding to the header-information. If the header states,
    that the file to be read contains two variables 'var1' and 'var2' of
    type 'double' and 'int' then it is important that they are given the
    right names 'var1' and 'var2' when read from the file, because
    typically the code following the read depends on the right names -
    like a database-lookup.

    Regards,
    Jesper
     
    Jesper Sahner, Nov 14, 2004
    #9
  10. Jesper Sahner <> scribbled the following:
    > Hi!


    > As I have described the task is somewhat similar to a database-lookup.


    > It is true that a variable is just a reference to an object or a
    > primitive type. However it is important that these variables have
    > names corresponding to the header-information. If the header states,
    > that the file to be read contains two variables 'var1' and 'var2' of
    > type 'double' and 'int' then it is important that they are given the
    > right names 'var1' and 'var2' when read from the file, because
    > typically the code following the read depends on the right names -
    > like a database-lookup.


    I still can't fathom why you think you have to use actual variable names
    for this. If the field names are so important, just use a big Map where
    the keys are the names "var1" and "var2" etc. and the values are Doubles
    or Integers or whatever objects you use to encapsulate what you read
    from the file.
    Trying to dynamically read or write variable names in the program source
    code at run-time is pretty much always a sign of a design problem or a
    misunderstanding of how compiled languages work.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "'So called' means: 'There is a long explanation for this, but I have no
    time to explain it here.'"
    - JIPsoft
     
    Joona I Palaste, Nov 14, 2004
    #10
  11. Jesper Sahner

    Oscar kind Guest

    Jesper Sahner <> wrote:
    > As I have described the task is somewhat similar to a database-lookup.
    >
    > It is true that a variable is just a reference to an object or a
    > primitive type. However it is important that these variables have
    > names corresponding to the header-information.


    Why do you need to use a variable? Why can't you attach that name to the
    value using an associative collection (i.e. a Map)?


    > If the header states,
    > that the file to be read contains two variables 'var1' and 'var2' of
    > type 'double' and 'int' then it is important that they are given the
    > right names 'var1' and 'var2' when read from the file, because
    > typically the code following the read depends on the right names -
    > like a database-lookup.


    Code cannot use a runtime-defined variable name, as the compiler cannot
    compile that (it cannot reference what doesn't exist). It can however, use
    a fixed variable to hold the name of another variable, and look up its
    value in a Map (and store that value in another fixed variable).

    Then again, if what you're doing is like a database lookup, why not use a
    database? If it is just for configuration, use a Properties object
    (similar to a Map<String,String>, but with load/store capabilities). If it
    is more complex, you often want to store business data, and then a
    database is most likely the best choice.

    So regardless of what you do: make sure your design is right. If you
    cannot give a good reason for each design choice, nor explain why it is
    better than the alternatives, you've made the wrong choice.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Nov 14, 2004
    #11
  12. Jesper Sahner

    Chris Smith Guest

    Jesper Sahner wrote:
    > As I have described the task is somewhat similar to a database-lookup.
    >
    > It is true that a variable is just a reference to an object or a
    > primitive type. However it is important that these variables have
    > names corresponding to the header-information. If the header states,
    > that the file to be read contains two variables 'var1' and 'var2' of
    > type 'double' and 'int' then it is important that they are given the
    > right names 'var1' and 'var2' when read from the file, because
    > typically the code following the read depends on the right names -
    > like a database-lookup.


    Unfortunately, you seem to be unable to transcend the specific
    implementation that you've decided that you want. Let me be entirely
    clear: what you want to do is not possible, nor even sensible, in Java.
    If you write code that uses 'var1' and 'var2' without declaring them
    yourself, then your code won't compile.

    Now, for the umpteenth time, what are you trying to do?

    Depending on what you're trying to do, perhaps the following would work:

    double var1 = Double.NaN, var2 = Double.NaN;

    As you're reading the file,

    if (name.equals("var1")) var1 = Double.parseDouble(value);
    else if (name.equals("var2")) var2 = Double.parseDouble(value);
    ...

    And then you can write your code:

    return var1 * Math.sin(var2);

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Nov 15, 2004
    #12
  13. Joona I Palaste wrote:

    > Trying to dynamically read or write variable names in the program source
    > code at run-time is pretty much always a sign of a design problem or a
    > misunderstanding of how compiled languages work.


    And it is rarely, if ever, good design even in languages where it might
    be made to work. (E.g. Perl) Such languages, by the way, must
    necessarily handle the situation internally in a manner similar to the
    Map-based approach suggested elsewhere in this thread for handling it in
    Java.


    John Bollinger
     
    John C. Bollinger, Nov 15, 2004
    #13
  14. Jesper Sahner

    Alberto Guest

    Although you are not describing too much about your project/assignment,
    it sounds to me that you are trying to build some sort of a small
    interpreter Somewhat like Perl and scalars (which instead of a
    compiler, it uses an interpreter). If this is your real goal, you need
    to remember that interpreters are really using a lot of abstractions.
    Most of them use a lookup table (although there are many other ways to
    simulate this) keeping record of the variables during the run-time
    (e.g. Hashmap, Hashtables). For all you care, their values (or even
    types if you want to get more fancy) can be stored as strings. These
    values can later be interpreted to their correct meaning according to
    your design.

    Once again, it would help you a lot if you start thinking of
    generating the abstraction that those two variables var1 and var2 are
    working together, rather than looking to have the compiler do the
    interpretation for you. Remember that in this realm of thinking about
    "abstractions", there are many approaches you could take.
     
    Alberto, Nov 16, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?S=F8ren_Bak?=

    Collections API for primitive types

    =?ISO-8859-1?Q?S=F8ren_Bak?=, Aug 27, 2003, in forum: Java
    Replies:
    0
    Views:
    416
    =?ISO-8859-1?Q?S=F8ren_Bak?=
    Aug 27, 2003
  2. munki
    Replies:
    5
    Views:
    3,117
    Michael Borgwardt
    Oct 8, 2003
  3. Replies:
    7
    Views:
    618
    Victor Bazarov
    May 9, 2005
  4. Daniel Pitts
    Replies:
    7
    Views:
    503
  5. Nasser M. Abbasi
    Replies:
    74
    Views:
    3,651
Loading...

Share This Page