你能告诉我for
下面的循环有什么问题吗?我希望程序扫描文本中的所有单词并检查它们 - 如果发现它与一组单词中的一个匹配,它会替换它。例如,如果文本中的句子是“nona chan”,我希望它逐字扫描,如果找到单词“nona”替换为“good”,然后继续下一个单词。
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(" ");
for (int i = 0; i <= t1.length; i++) {
do {
if (t1[i].equals("nona")) {
String v1 = t1[i];
String v2 = " good ";
String a = v1.replace(v1, v2);
te2.setText(a);
} else if (t1[i].equals("chan")) {
String v1 = t1[i];
String v3 = " job ";
String a = v1.replace(v1, v3);
te2.setText(a);
}
} while (te1.length() != 0);
}
}
});
}
@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;
}
}