How to check if variable name contains string and then output string variable content

java, string

Solution

The data type you're looking for is `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 a unique key to a value. In this case both have the type `String`. You add bindings using `put(key, value)` and get the binding for a key using `get(key)`.

Problem

So I 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 I want to be able to do is to get a string and compare it to the variable and then out put what the variable contains if it equals it. For example if my string equals "ART_DEPT" then it check if there is a variable called ART_DEPT and then output "VISUAL ARTS" I was thinking of putting it in a 2D String array or a list but I'm not really sure as to how to do what I want to do

Original source

Related problems