Results 1 to 5 of 5
  1. #1
    nadissen is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Post Dynamic variable name based on other variable

    Here is my problem:

    System.out.print("Enter a string");
    String word = input.next(); -> Assume input is a scanner already defined

    Object word = new Object (...); -> this word here is the data found in String word
    How can i get the data found in String word and pass it as a name for another object??
    Glad to be of help
    (if you could check on my problems too :) )

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    You can't.

    Maybe you want a Map.

    db

  3. #3
    nadissen is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    what do you mean by a map?
    you mean something like create a hashmap and pointing my 1st object to the 2nd one?
    Glad to be of help
    (if you could check on my problems too :) )

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

    Default

    Quote Originally Posted by nadissen View Post
    what do you mean by a map?
    you mean something like create a hashmap and pointing my 1st object to the 2nd one?
    Yes, a HashMap would work great for what it seems like you want to do: associate an object with a specific String. Also consider using other collections such as ArrayLists and even arrays, if you want to associate an object with a numeric index.

  5. #5
    nadissen is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    hmm ya i thought a bit about it, it might solve my problem, thanks a lot
    Glad to be of help
    (if you could check on my problems too :) )

Similar Threads

  1. "dynamic variable naming" ?
    By N00Bie in forum New To Java
    Replies: 7
    Last Post: 02-18-2011, 05:10 PM
  2. Alternating Transparency Based on Double Variable
    By YellowPainting in forum Java 2D
    Replies: 2
    Last Post: 11-05-2010, 09:59 PM
  3. How to update with dynamic variable?
    By mr_anderson in forum JDBC
    Replies: 9
    Last Post: 08-09-2010, 09:40 AM
  4. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  5. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 11:38 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
  •