String comparison does not work

Asked

Viewed 134 times

0

I have a string that currently only gets "ERROR" and I did an if for when she gave me this value ran some things, it turns out that the comparison always fails, even the strings being exactly the same, I always compared string this way, I do not understand why it is not working:

                           try {
                                String mensagem = response.body().getAsJsonObject().get("error").getAsJsonObject().get("msg").toString();
                                String status = response.body().getAsJsonObject().get("status").toString();
                                //Log.i(TAG, "onResponse: "+mensagem);
                                Log.i(TAG, "onResponse: "+status);
                                if(status.equals("ERROR")){
                                    Toast.makeText(SplashScreen.this, mensagem, Toast.LENGTH_SHORT).show();
                                    habilitarformgerarsenha(true);
                                }else{
                                    Log.i(TAG, "onResponse: Status mensagem:" + status);
                                }
                            }catch (NullPointerException e){
                                Log.i(TAG, "onResponse: "+e);
                            }

LOG:

02-05 23:57:41.036 30711-30711/1.com.br.doup I/igr: onResponse: "ERROR"
02-05 23:57:41.036 30711-30711/1.com.br.doup I/igr: onResponse: Status mensagem:"ERROR"
  • Do not capture NullPointerException. Instead see which line you are accessing something null, and give resolution to that case.

  • @Isac pulled the Try catch, nothing’s changed

  • It is not supposed to change unless you specify the line where the problem actually is. Now it is supposed to show the error you gave in the log and its line

  • No error, does not fall in catch, it always falls in Else, even though the condition is true

  • Log.i(TAG, mensagem + "," + status); before the if shows what ?

  • I/Igr: "Invalid user!" ,"ERROR"

  • But the status has ERROR or "ERROR" ?. Pay attention because quotation marks make a difference, and if that’s the case you’ll never get true

  • Has quotes yes it looks like "ERROR" is a string

  • Yes apparently his String already have quotes inside. You can easily test by changing your if for if(status.equals("\"ERROR\"")){

  • @Isac was this very thing, put as answer, if possible how to remove the inside quotes

Show 5 more comments

1 answer

1


By what appears in the Log:

onResponse: "ERROR"

To String that has in status It’s already got quotes in it which makes the if:

if(status.equals("ERROR")){

Never give true.

To resolve can change the if to include quotes as well:

if(status.equals("\"ERROR\"")){

Note that they had to be included with \" to escape the opening quotation marks.

Or another alternative would be to remove the quotation marks that are already there at the expense of substring:

status = status.substring(1, status.length()-1); //remover as aspas
if(status.equals("ERROR")){ //if normal

The substring used handhold from the second character, at position 1, to the penultimate, given by length - 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.