Results 1 to 2 of 2
  1. #1
    marklee42 is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Question String -> variable (setting variables from parsing a text file)

    I'm trying to set variables from a text file. If the string I get is exactly the same name as a variable in my class, can I set it directly? For example:

    int myValue;

    String s = "myValue";
    int i = Integer.parseInt("42");

    Is there a better way to assign the value besides doing:

    if (s.equals("myValue"))
    myValue = i;

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Is there a better way to assign the value besides doing:
    The best way is the way that works. Does this work for what you are trying to accomplish? There are quite possibly many different way to accomplish what you are doing, the 'best' way is quite relative to what you are trying to accomplish in the grand scheme of things.

Similar Threads

  1. Store links in text file and read into variables
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 03-31-2011, 05:18 PM
  2. setting path and classpath variables from batch file -
    By manojsingh.manoj@gmail.co in forum Advanced Java
    Replies: 1
    Last Post: 09-15-2009, 11:50 AM
  3. remove variables/line in a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-04-2009, 03:05 AM
  4. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 10:17 AM
  5. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 04:47 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
  •