java - How to check if variable name contains string and then output string variable content -
so have these 4 variables
private final string prog_dept = "programming/engineering"; private final string des_dept = "design/writing"; private final string art_dept = "visual arts"; private final string sound_dept = "audio";
what want able string , compare variable , out put variable contains if equals it.
for example if string equals "art_dept" check if there variable called art_dept , output "visual arts"
i thinking of putting in 2d string array or list i'm not sure how want do
the data type you're looking map<string, string>
.
map<string, string> departmentnames = new hashmap<string, string>(); departmentnames.put("prog_dept", "programming/engineering"); departmentnames.put("des_dept", "design/writing"); //...etc... //... string dept = "prog_dept"; string deptname = departmentnames.get(dept); system.out.println(deptname); //outputs "programming/engineering"
a map
binds unique key value. in case both have type string
. add bindings using put(key, value)
, binding key using get(key)
.
Comments
Post a Comment