2

My code need the if statement to see the last character of some string but how many time i try nothing happens, that mean the conduction is false !.

my code:

            String zzz = T2.getText().toString();
            String zz = zzz.substring(zzz.length() - 1);

            String oo = "+";
            if ( zz ==  oo){         
                T1.setText(" the result ");
            } 
            ..
            String Add1 = T1.getText().toString();
            AAAA11111 = AAAA11111 + Double.parseDouble(Add1);
            T2.setText(Double.toString(AAAA11111) + " +"); // the last character is "+" and it's what i needed
            ..
khaledgg7
  • 35
  • 1
  • 8

2 Answers2

3

For string comparison use equals method:

zz.equals(oo)

In this answer you will find a very good explanation about the difference between using the "==" operator and the equals method for string comparison.

Pablo
  • 2,581
  • 3
  • 16
  • 31
1

If you only want to check the last character of the string, It also works!

char lastChar = zzz.charAt(zzz.length() - 1);

if (lastChar == '+') {
    // Do something
}
Tura
  • 1,227
  • 11
  • 22
  • By the way, I think the difference between equals() and == operator is bad boilerplate code of Java.. (Not support to operator overloading) Many developers make mistakes for that. – Tura Feb 19 '18 at 01:30
  • it doesn't working in android java but the "equals" worked, thank you all – khaledgg7 Feb 19 '18 at 02:13