我比較2 EditText框的字符串時有一些問題。 這裏是JAVA:JAVA(Android) - 比較兩個字符串時,即使它們相同它返回false
public void signUpSubmit(View v){
ErrorBox.setText("");
String eAdd = EmailAddress.getText().toString();
String eAddConf = ConfirmEmail.getText().toString();
String pass = Password.getText().toString();
String passConf = ConfirmPassword.getText().toString();
String fName = FirstName.getText().toString();
String lName = LastName.getText().toString();
Boolean emailSame;
Boolean passSame;
Boolean emailEmpty;
Boolean passEmpty;
Boolean fNameEmpty;
Boolean lNameEmpty;
if(eAdd.equals(eAddConf)){
emailSame = true;
}else{
emailSame = false;
}
if(pass.equals(passConf)){
passSame = true;
}else{
passSame = false;
}
if(eAdd.equals("")){
emailEmpty = true;
}else{
emailEmpty = false;
}
if(pass.equals("")){
passEmpty = true;
}else{
passEmpty = false;
}
if(fName.equals("")){
fNameEmpty = true;
}else{
fNameEmpty = false;
}
if(lName.equals("")){
lNameEmpty = true;
}else{
lNameEmpty = false;
}
Boolean noErrors;
String ErrorCode = null;
if(emailEmpty==true){
noErrors=false;
ErrorCode = "Email is Empty";
}else if(fNameEmpty==true){
noErrors=false;
ErrorCode = "First name is Empty";
}else if(lNameEmpty==true){
noErrors=false;
ErrorCode = "Last Name is Empty";
}else if(passEmpty==true){
noErrors=false;
ErrorCode = "Password is Empty";
}else if(emailSame==true){
noErrors=false;
ErrorCode = "Emails Don't Match";
}else if(passSame==true){
noErrors=false;
ErrorCode = "Passwords Don't Match";
}else{
noErrors=true;
}
if (noErrors==false){
ErrorBox.setText(ErrorCode);
}else{
String signUpStatus = signUpHttp(eAdd, pass, fName, lName);
if (signUpStatus.equals("Error")){
ErrorBox.setText("Server Down, Please Try Again Later");
}else if (signUpStatus.equals("False")){
ErrorBox.setText("That Email has already been used");
}else if (signUpStatus.equals("True")){
MainActUN.setText(eAdd);
MainActPW.setText(pass);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
我確實有重複,如果在自己的函數語句,但我這樣做是爲了看它是否會解決我的問題,但它並沒有任何幫助是極大的讚賞。
編輯----------------------------------
對不起,沒有具體的,如果所有的文本框被填充,我得到的電子郵件不匹配,我已經刪除了電子郵件測試,同樣的事情發生,說密碼不匹配,但其餘與.equals(「」)是好的。
該冗餘代碼冗餘且由於冗餘而難以閱讀。什麼,具體不工作?他們都不工作? – nhgrif
事實上是多餘的,爲什麼不具體說明哪兩個編輯文本和代碼中出現意外行爲的地方。 – ChiefTwoPencils
如果...請刪除所有這些!只需使用例如「emailSame = eAdd.equals(eAddConf);」這將使代碼更容易閱讀和理解! – isnot2bad