lundi 26 février 2018

if statement with not equals in Java android

I have this code the idea is to take the three strings and make sure that they are same strings and ALSO they are not null or empty (if it empty so it will be same), So I wrote this code that and it's working fine in java online compiler but not working in Android studio (the condition is false i think!) or because I put the if outside so it doesn't working!

 int i = 0; ...

    B1 = (Button) findViewById(R.id.B1);
    B2 = (Button) findViewById(R.id.B2);
    B3 = (Button) findViewById(R.id.B3);

    B1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if (i==0){
                B1.setText("X");
                i++;}
            else if (i==1){
                B1.setText("O");
                i = 0;}
        }
    });

  // the same for B2 and B3...

    String SB1 = B1.getText().toString();
    String SB2 = B2.getText().toString();
    String SB3 = B3.getText().toString();
    if (SB1.equals(SB2) && SB2.equals(SB3) && !SB1.equals("") ){
        Win.setText("win");
    }

Aucun commentaire:

Enregistrer un commentaire