javaandroideclipseequalsequals-operator

Equal function is not working in my java code


I have 2 textviews, 1 editbox and 1 button in my xml.

In my code, this line is not working:

if(c.equals(str)){
                Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
            }

But next line (else statement) is working and In both cases (if & else statement) it shows else function toast ("Wrong").

Here is my code:

public class GameActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { 
            TextView tv = (TextView) findViewById(R.id.textView1);
            TextView tv2 = (TextView) findViewById(R.id.textView2);
            Random r = new Random();
            int i = r.nextInt(10 - 2) + 2;
            tv.setText(i +"");
            Random r1 = new Random();
            int j = r1.nextInt((9 - 2) + 1) + 2;
            tv2.setText(j +"");
            
            int a = Integer.parseInt(tv.getText().toString());
            int b = Integer.parseInt(tv2.getText().toString());
            int result =  a*b;
            String str = String.valueOf(result);
            
            EditText txt4 = (EditText) findViewById(R.id.editText1);
            String c = txt4.getText().toString();
            
            if(TextUtils.isEmpty(txt4.getText().toString())) {
                txt4.setError("Please enter your answer");
                return;
            }
            
            if(c.equals(str)){
                Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
                
            }
            
            else{
                Toast.makeText(GameActivity.this, "wrong",Toast.LENGTH_LONG).show();
            }
            
            txt4.setText("");
        }

    });
    }

Solution

  • Try this, I got result

    public class Test extends AppCompatActivity {
    Button b1;
    TextView t1,t2;
    EditText e1;
    int a,b;
    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
         b1 = (Button) findViewById(R.id.button2);
         t1=(TextView) findViewById(R.id.textView4);
         t2=(TextView) findViewById(R.id.textView5);
         e1=(EditText) findViewById(R.id.editText);
        Random r = new Random();
        int i = r.nextInt(10 - 2) + 2;
        t1.setText(i +"");
        Random r1 = new Random();
        int j = r1.nextInt((9 - 2) + 1) + 2;
        t2.setText(j +"");
    
         a = Integer.parseInt(t1.getText().toString());
         b = Integer.parseInt(t2.getText().toString());
    
      b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                int result =  a*b;
                String str = String.valueOf(result);
    
                String c = e1.getText().toString();
    
                if(TextUtils.isEmpty(c)) {
                    e1.setError("Please enter your answer");
                    return;
                }
    
                if(c.equals(str)){
                    Toast.makeText(Test.this, "Alright",Toast.LENGTH_LONG).show();
    
                }
    
                else{
                    Toast.makeText(Test.this, "wrong",Toast.LENGTH_LONG).show();
                }
    
                e1.setText("");
            }
    
        });
    }}