我正在使用 IF 语句来检查值 MobileNumber 是否以 07 开头。这是我的代码:
public void MobileNumberCheck(EditText MobileNumber_Text, TextView ErrorDisplay, Boolean Output) {
Editable MobileNumber = MobileNumber_Text.getText();
Output = false;
///This method only has to check that the mobile number starts with 07, as the XML specifies the number can only be number, and 11 digits long.
if( (MobileNumber.charAt(0) != "0".charAt(0) ) || (MobileNumber.charAt(1) != "7".charAt(0)) ){
ErrorDisplay.setText("Invalid UK mobile number");
}
else if( (MobileNumber.charAt(0) == "0".charAt(0)) && (MobileNumber.charAt(1) == "7".charAt(0)) ){
///Do nothing
ErrorDisplay.setText("");
Output = true;
}
else
ErrorDisplay.setText(MobileNumber.charAt(0) + MobileNumber.charAt(1));
}
public void AddPeople_Save(View view){
TextView ErrorDisplay = (TextView) findViewById(R.id.AddPeople_ErrorDisplay);
EditText MobileNumber_ET = (EditText) findViewById(R.id.AddPeople_MobileNumber);
Boolean WriteReady = false;
MobileNumberCheck(MobileNumber_ET, ErrorDisplay, WriteReady); ///Runs the method specified above. Doesn't output, except into the TEXTVIEW ErrorDisplay.
EditText Name_ET = (EditText) findViewById(R.id.AddPeople_Name);
String AddPeople_PeopleFilename = "PartyApp_PeopleFile";
String Person_Details = Name_ET.toString() + MobileNumber_ET.toString();
FileOutputStream outputStream;
if(WriteReady == true)
try {
outputStream = openFileOutput(AddPeople_PeopleFilename, Context.MODE_PRIVATE);
outputStream.write(Person_Details.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
else{
///Do nothing. The error message is passed through METHOD MobileNumberCheck.
ErrorDisplay.setText("AAA" + WriteReady);
}
你能告诉我我做错了什么吗?我假设我的逻辑运算符 && 和 || 是错误的,或者string.CharAt()
正在执行错误。感谢!!