Answer the question
In order to leave comments, you need to log in
There is a message with a field of type string, can I parse this field?
there is a field (string type) "value": "OK" / "Error" - that is, two possible values. Can I deduce something appropriate based on the value. For example, if "OK" came, write "everything is good", and if "Error" came, write "something wrong". Is it generally normal practice to parse a string? Or do you need to use boolean type or int for this?
Answer the question
In order to leave comments, you need to log in
if(value.equals("OK")) { ... }
switch(value) {
case "OK": ... break;
case "Error": ... break;
}
boolean
or enum
-types can be used instead (if more than two values).enum Status {
Ok,
Error,
Warning
}
class Foo {
static void bar(Status status) {
if (status == Status.Ok) {
...
}
...
}
}
If the final result will have the value yes/no, OK/error, true/false it is better to use boolean, if there are more than two values - int.
In general, using string as a success/fail indicator is a bad idea.
It's better to use string as value decoding. Let's say boolean value is false, string value "Operation failed, boolean - true, string value "Operation completed successfully".
Make the method void. In case something can go wrong in the logic of the method, neither boolean nor int should be returned. Throw an exception.
As a result, you will get:
If OK - just execute the logic
If Error - throw Exception
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question