-2

I am trying to find a way to convert a String value into a variable. Is this possible?

For example:

String str = "chicken";
/*Then have it so I can do this*/
int chicken = 2;

I know I can just type the variable name in, but I want it so a user input, which is a String, can be converted into a variable name after a button is clicked. Here is what I mean:

/*This is in JavaFX*/
Button button = new Button("Click Me");
TextField userText = new TextField();
ArrayList<List> randomList = new ArrayList<>();

/*When button is clicked*/
button.setOnAction(e -> {
    ArrayList<List> "The user's text" = new ArrayList<>();
    randomList.add("The user's text");
});

"The user's text" is the user input being used as a variable name.

P.S. = Please don't recommend HashMaps or Maps unless there is no other way. I've already started with ArrayLists.

Danielson
  • 2,605
  • 2
  • 28
  • 51
Max Echendu
  • 203
  • 1
  • 3
  • 8
  • 1
    I'm pretty sure you can use the [Reflection API](http://docs.oracle.com/javase/tutorial/reflect/index.html), but then you'd need to use reflection each time you want to access that variable, which would be inefficient and messy. It really would be better to use a Map. – Reinstate Monica -- notmaynard Jul 29 '15 at 14:19
  • 3
    Well, the fact that you started with `ArrayList` has nothing to do with it. There is no way in Java to convert strings to variable names. Java is not a scripting language and variable names are determined at compile time. So a `Map` is your only option. – RealSkeptic Jul 29 '15 at 14:19
  • 1
    What do you mean with "variable name". Variable names are only visible in the source code and therefore not visible at runtime when a user enters the value. Therefore your question does IMHO not much sense. – Robert Jul 29 '15 at 14:20
  • Variables are made to represent data or structures that the developer can use by referring to its name. There is quite no point in letting the user decide the name of a variable. Mostly because you can't refer to it if you don't know its name :) – dotvav Jul 29 '15 at 14:21
  • Also, look closer at your example code: you don't use the variable name after you feel you need it. You could have named that variable `x` for all it matters. If you're trying to do something *different from `randomList.add(new ArrayList<>())`*, please [edit] your question to say what you're *really* trying to do. – Dan Getz Jul 29 '15 at 14:21
  • 1
    A `Map` is the only way to go – fantaghirocco Jul 29 '15 at 14:22
  • @dotvav Actually, it's just a matter of the language background you come from. In PHP, for example, you can do exactly what the user is asking for: Have "variable variables", where the string the user enters actually gets saved in the symbol table as the name of a variable and can be used directly or indirectly via its string name. – RealSkeptic Jul 29 '15 at 14:29

1 Answers1

0

I think we could better help you if we understood what you wanted to accomplish. Without that, I would suggest using an object. Using a Map is another option, but in general it's going to make your code less readable, and if your structure changes, it will be more difficult to maintain a Map.

public class UserData {
    private String userInput;
    private List<List<?>> data = new ArrayList<>();

    // Getters and Setters
}

Then use it like this:

/*When button is clicked*/
button.setOnAction(e -> {
    UserData userData = new UserData();
    userData.setUserInput("The user's text");
    randomList.add(userData);
});
Samuel
  • 16,923
  • 6
  • 62
  • 75