Borut Flis
Borut Flis

Reputation: 16375

How to set input as a name for a variable in java?

System.out.print(">> ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();

I would like to name a variable (double input=new double[5];) with the name the program gets from BufferedReader. How to do that?

Upvotes: 3

Views: 3032

Answers (2)

Bozho
Bozho

Reputation: 597124

You can't do it. Variable are defined at compile-time. And you don't need it - how would you access variables with dynamic names?

You can use a Map<String, double[]> to map a string to a double array. map.put(name, array) and then map.get(name) will give you the array.

Upvotes: 4

Morten Kristensen
Morten Kristensen

Reputation: 7613

No, you can't. It would be equivalent of changing the source code at run-time. Why do you need to do this, anyway?

Upvotes: 1

Related Questions