Results 1 to 10 of 10
Thread: Convert variable name to string.
- 08-16-2010, 11:02 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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
basically i want to do something likeJava Code:[variable1] :value [variable2] :value1:value2:value3
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?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] }Last edited by MHardeman25; 08-16-2010 at 11:09 PM.
- 08-16-2010, 11:05 PM #2
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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.
-
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!
- 08-16-2010, 11:17 PM #4
The only way I can think of is to use the String as a Key in a Map.way to convert strings to variable names
- 08-17-2010, 02:49 AM #5
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.
- 08-17-2010, 03:03 AM #6
I'm not sure. Look in a .class file with a hex editor. The variable names are there.the compiler optimizes out variable names
- 08-17-2010, 03:40 AM #7
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?
- 08-17-2010, 08:01 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-17-2010, 12:55 PM #9
I use javac.exe
- 08-17-2010, 09:18 PM #10
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
-
convert obj to string
By isme in forum New To JavaReplies: 11Last Post: 06-14-2010, 10:54 AM -
convert number to string
By wizard_oz in forum New To JavaReplies: 9Last Post: 11-16-2009, 07:04 PM -
trying to convert an interger to a string
By matpj in forum New To JavaReplies: 12Last Post: 01-15-2009, 10:08 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 04:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks