C
C
Carlyndow Marlifi2021-12-24 00:13:33
Kotlin
Carlyndow Marlifi, 2021-12-24 00:13:33

Java / Kotlin: why can't this be done?

Why, for example, can't I let the user enter any arbitrary value, and then find such a value and give a decryption?

<resources> // это в values/strings.xml
    <string name="bomba">z2df21ffg231ffz</string>
</resources>

// это в MainActivity.kt

    var userInputValue = "bomba"
    var secretKey = R.string.userInputValue

What is the point?
The person in the field enters the word "bomba", and he is given the secret key "z2df21ffg231ffz"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Kudryavtsev, 2021-12-24
@petushok55

It is possible to get an integer identifier from a string, but this is unproductive, so use with caution.
UPD:
You should not do this until you are one hundred percent sure of what you are doing. It is better to reconsider the approach to the solution.

val resId = context.resources.getIdentifier("bomba", "string", packageName)
if (resId != 0) {
    val secretKey = context.getString(resId)
    // ...
}

Documentation:
https://developer.android.com/reference/kotlin/and...

O
Oleg, 2021-12-24
@402d

HashMap<String, String> map = new HashMap<String, String>();
map.put("car", "drive");
map.put("boat", "swim");

map.get("car")

S
Sergey, 2021-12-24
@red-barbarian

because when assembled, R.string.bomba becomes an integer constant

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question