mardi 13 juillet 2021

Use value of a String variable as a variable name in Java

What I'm trying to do is use a String from a text file as a variable name. Example code to show what I'm tryng to do:

try{
    String s = "Thermostat,1000,1000";
    String[] event = s.split(",");
    Class event[0] = Class.forName("Greenhouse.Event");
catch(Exception e){
    e.printStackTrace();
}

Note that this happens multiple times in the file and I save the names to a list for later use.

Edit: Looking further into it it would also be possible to use numbered variables with the only problem being I wouldn't know in advance how many there'd have to be.





Aucun commentaire:

Enregistrer un commentaire