0
final EditText d1n = (EditText) findViewById(R.id.d1n);

AA coming from edittext.

NotHesapla(d1n.getText().toString()); 

returns -1. if I try to

NotHesapla("AA");

working succesfully.

public double NotHesapla(String not) {
    double puan;
    if (not == "AA") { puan = 4; }
    else if (not == "BA") { puan = 3.5; }
    else if (not == "BB") { puan = 3; }
    else if (not == "CB") { puan = 2.5; }
    else if (not == "CC") { puan = 2; }
    else if (not == "DC") { puan = 1.5; }
    else if (not == "DD") { puan = 1; }
    else if (not == "FD") { puan = 0.5; }
    else if (not == "FF") { puan = 0; }
    else { puan = -1; }
    return puan;
}

How can I solve this issue? Thank you

ed joe
  • 91
  • 1
  • 6
  • 8
    Noooooooooooooooooooooooooooooooooooooooooooooooo, don't compare String values using `==` !!!!!!!!!!!!!!! Use `equals()`. – user2336315 Jan 09 '14 at 09:50
  • When using == with String you are comparing references. Not the actual text. Use equals() – stefana Jan 09 '14 at 09:55

3 Answers3

4

To compare strings you need to use equals():

if( not.equals("BA") )...

or better (to avoid NPE):

if( "BA".equals(not) )...
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

You should be using equals method instead of == opertaor.

if (not.equals("AA")) { puan = 4; }

Note that:

== tests for reference equality.

.equals() tests for value equality.

Please see here for more information.

Community
  • 1
  • 1
Bhushan
  • 6,151
  • 13
  • 58
  • 91
1

For Strings always use the equals method !

public double NotHesapla(String not) {
double puan;
if ("AA".equals(not)) { puan = 4; }
else if ("BA".equals(not)) { puan = 3.5; }
else if ("BB".equals(not)) { puan = 3; }
else if ("CB".equals(not)) { puan = 2.5; }
else if ("CC".equals(not)) { puan = 2; }
else if ("DC".equals(not)) { puan = 1.5; }
else if ("DD".equals(not)) { puan = 1; }
else if ("FD".equals(not)) { puan = 0.5; }
else if ("FF") { puan = 0; }
else { puan = -1; }
return puan;}
Sabbe
  • 21
  • 3