Results 1 to 10 of 10
  1. #1
    MHardeman25 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default Convert variable name to string.

    Ok, I am reading a text file, the file contains variables and the values I want to assign to them. Is there any way to convert strings to variable names.

    Example text file
    Java Code:
    [variable1]
    :value
    
    [variable2]
    :value1:value2:value3
    basically i want to do something like
    Java Code:
    String[] varNames = (get input from txt file);
    String[] vals = (get input from txt file);
    for(int index = 0; index < varNames; index++){
         (convert varNames[index] to variable) = vals[get input]
    }
    i know i would have to handle it differently if the val was an array. This is just an example, but is there any way to do this?
    Last edited by MHardeman25; 08-16-2010 at 11:09 PM.

  2. #2
    MHardeman25 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Also, this is going to be inplimented in a lot of classes with any number of variables the text file could be edited by users and stuff so I really want this to be able to handle abuse. I can't just make due with

    Java Code:
    String variable1;
    if("variable1" == (input from txt file){
          variable1 = vals[whatever];
    }
    Last edited by MHardeman25; 08-16-2010 at 11:10 PM.

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

    Default

    One way to associate a String with an object of another class is to use a Map, i.e., Map<String, OtherClass>.

    Also, is the text file in XML form? If so you can unmarshal the XML data into objects with JAXB (Java Architecture for XML Binding).

    Can you describe what problem you're trying to solve in a general non-Java sense? If so, perhaps there's a better way than what you're trying to do.

    Finally, how your code handles "abuse" will be up to you.

    Good luck!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    way to convert strings to variable names
    The only way I can think of is to use the String as a Key in a Map.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It is also worth noting that the compiler optimizes out variable names. What goes in as someVariableName might come out as something completely obfuscated (as short as one letter) to save processing time, storage space, and provide a low level of security against decompiling.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    the compiler optimizes out variable names
    I'm not sure. Look in a .class file with a hex editor. The variable names are there.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Static variables and function names are. I cannot find any local variables in my class files. Perhaps different compilers do different things... are you using NetBeans?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Static variables and function names are. I cannot find any local variables in my class files. Perhaps different compilers do different things... are you using NetBeans?
    The 'debug level' determines what is stored in a class file and what not. The -g:none flag leaves out all debug data. See javac -help for all the options.

    kind regards,

    Jos

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I use javac.exe

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It would seem then, that NetBeans provides a low level of obfuscation to local/member variables whereas javac does not unless, as Jos stated, the -g:none flag is used.

Similar Threads

  1. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  2. convert number to string
    By wizard_oz in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 07:04 PM
  3. trying to convert an interger to a string
    By matpj in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 10:08 AM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 04:30 AM

Posting Permissions

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