-1

The code works fine it takes the contant split when there is a space and replace what ever word the user enter to the word in V2 the problem is when i put the IF statment to check the word the user entered it does not work whats wrong with the if ?

package com.example.split;



import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

        final EditText te1 = (EditText)findViewById(R.id.t1); 
        final EditText te2 = (EditText)findViewById(R.id.t2); 

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //imva.setImageResource(R.id.b1);

      String t=te1.getText().toString(); 
      String[] t1= t.split(" ");

      if (t1[0] == "hello")
      {
       String v1= t1[0];
       String v2 = " true ";  
       String a =  v1.replaceAll(v1, v2);
       te2.setText(a);  
      }      
      }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Ahmad
  • 69,608
  • 17
  • 111
  • 137

2 Answers2

3

Change

if (t1[0] == "hello"){...}

to

if (t1[0].equals("hello")){...}
Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

1)

if (t1[0] == "hello")

don't ever compare Strings and Objects like that. That way you can compare only object references, not the contents

Java String.equals versus ==

2)

v1.replaceAll(v1, v2);

Takes first argument as a regular expression. And I doubt that is what you want. I bet you want

v1.replace(v1, v2);

http://developer.android.com/reference/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99