Home > Blockchain >  Description for String[] entries
Description for String[] entries

Time:01-13

so i'm working on a project and i need the following to work:

Let's say I have a String[] contatining out of f.e 3 values "0D", "0A", "01A0" Now in the background I got like a defined description for each of these values and I want to show them in another string. So in the end i want to call a method with String"0D" and the method returns me the description, in this example "speed" same for the others, if i call the method with "0A" it returns String "Fuel Pressure"

Is there an efficient way for achieving this? Cause i've got a pretty long list and don't want to manually input all the descriptions to the commands..

CodePudding user response:

I would consider using a hashmap of <String, String>. The key would be the command, and the value is the description.

CodePudding user response:

Yeah a HashMap would work. You could try this:

HashMap<String, String> valueDescription = new HashMap<>();
valueDescription.put("0D", "speed");
valueDescription.put("0A", "Fuel Pressure");
valueDescription.put("01A0", "Temperature");

public String getDescription(String value) {
    if (valueDescription.containsKey(value)) {
        return valueDescription.get(value);
    } else {
        return "Description not found";
    }
}
  • Related