Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 640
Borut Flis Avatar asked Mar 21 '26 14:03

Borut Flis


2 Answers

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.

like image 182
Bozho Avatar answered Mar 24 '26 03:03

Bozho


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

like image 29
Morten Kristensen Avatar answered Mar 24 '26 03:03

Morten Kristensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!