简体   繁体   中英

Is there any way I can output the variable name instead of the value? [JAVA]

for example, I have a list of integer variables

int CLUTCH_CASE = 8;
int CS20 = 6;
int DANGER_ZONE_CASE = 6;
int TOTAL_CASE = 0;

I have an array:

int CaseValue[] = {CS20, CLUTCH_CASE, DANGER_ZONE_CASE};

and I output the value of total cases, which is going to be:

for (int counter=0;counter<CaseValue.length;counter++) {
    TOTAL_CASE += CaseValue[counter];
}
System.out.println("You have "+CaseValue.length+" CSGO Cases");
System.out.println("You have a total of "+TOTAL_CASE+" CSGO Cases:");

Output:

You have 3 CSGO Cases
You have a total of 20 CSGO Cases:

yet I also want to output the name.

Example: (CASENAME are just placeholders for this example. I wanted to use CaseValue[].NAME even that does not exist since it's an example)

for (int counter2=0;counter2<CaseValue.length;counter2++) {
    System.out.println(CASENAME+": "+CaseValue[counter2]);
}

My expected and wanted output to be:

CLUTCH_CASE: 8
CS20: 6
DANGER_ZONE_CASE: 6

Is there any way to output the name of the integer variable?

There is no command in Java to access the variable name.

You could use a map (like stated in other answers), but I'd also like to present a simple possibility that is possible without advanced features of Java.

What you could do is the following:

String[] caseName = {"CS20", "CLUTCH_CASE", "DANGER_ZONE_CASE"};

After that you can do:

for (int i=0; i<CaseValue.length; i++) {
    System.out.println(caseName[i] + ": " + CaseValue[i]);
}

Use a Map.

Map<String, Integer> ma = new Hashmap()
for (int counter=0;counter<CaseValue.length;counter++) {
    TOTAL_CASE += CaseValue[counter];
    ma.put(<your_key>, <your_value>)
}

I still dont understand your code completely. So cannot give you a full working snippet. I have however added a snippet on how you could possibly use a map. Store the key as the name that you want and its corresponding value in that key of the map.

You need to use java.lang.Class and its getFields() method to return an array of java.lang.reflect Field[] class object Below will give you an idea of how, go take a look in the API docs for a better more suitable set of methods to design the process with a List or Set to use contains() method so you can put it in a class and call when you want in a program.

import java.lang.Class;
import java.lang.reflect.*;

Field[] varFset = ((java.lang.Class)this).getFields();
// compare names processing code over the array here
String varname = varFset[x].getName();

You could use a HashMap.

enum CaseType {
   CLUTCH_CASE,
   CS20,
   DANGER_ZONE_CASE;
}
Map<CaseType, Integer> map = new HashMap<>(3);
map.put(CaseType.CLUTCH_CASE, 8);
map.put(CaseType.CS20, 6);
map.put(CaseType.DANGER_ZONE_CASE, 6);
map.forEach((key, value) -> System.out.println(key.name() + ": " + value));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM