-2

I have a problem with getText() method. When I run this app and enter any number, also 9 it displays vcx. Is my if statement invalid or what?

public class Quadratic extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_quadratic, container, false);



    // layout
    Button calc = (Button)rootView.findViewById(R.id.calcbtn); //main  button
    final EditText etfx2 = (EditText)rootView.findViewById(R.id.etfx2); // x^2 etf

    final TextView tbefs = (TextView)rootView.findViewById(R.id.tbefs);



    calc.setOnClickListener(new View.OnClickListener()
    {

        public void onClick (View v){
            String a= etfx2.getText().toString();


            if(a=="9")
            {
                tbefs.setText("asd");}
            else
            {
                tbefs.setText("vcx");
            }


        }

    });
    return rootView;
}}
codeMagic
  • 44,549
  • 13
  • 77
  • 93
Convaly
  • 49
  • 1
  • 2
  • 4
  • 1
    Use `.equals()` and not `==` to compare strings. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Ken Wolf Oct 02 '14 at 13:32
  • anyone knows why java developers don't deal with this string equality issue? even I saw 10+ posts about this in SO. – İsmet Alkan Oct 02 '14 at 13:43

2 Answers2

0

Instead of a == "9" use:

a.equals("9")

And you also got something to ignore case

a.equalsIgnoreCase("9")
Kiraged
  • 519
  • 2
  • 9
  • 28
0

When using Strings, if you write a=="9", you compare the references. It means 'Is the string pointed by a the same string pointed by "9" ' i.e. are a and "9"stored in the same place in the memory.

Here you want to compare the content of your string so you have to use the equals method.

if("9".equals(a)){
    // do something
}
jhamon
  • 3,603
  • 4
  • 26
  • 37