Android String.charAt()未返回预期值
我使用IF语句检查MobileNumber的值是否以07开头。这是我的代码:Android String.charAt()未返回预期值,android,string,operators,logic,charat,Android,String,Operators,Logic,Charat,我使用IF语句检查MobileNumber的值是否以07开头。这是我的代码: public void MobileNumberCheck(EditText MobileNumber_Text, TextView ErrorDisplay, Boolean Output) { Editable MobileNumber = MobileNumber_Text.getText(); Output = false; ///This method only
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()
的实现是错误的。谢谢
此方法只需检查手机号码是否以07开头,因为XML指定号码只能是数字,长度为11位
我只想使用:
if(MobileNumber.toString().startsWith("07") && MobileNumber.length() <= 11){
}
if(MobileNumber.toString().startsWith(“07”)和MobileNumber.length()if(num.startsWith(“0”)和num.startsWith(“7”,1))
或
如果(num.startsWith(“07”))
可以工作。string.CharAt()实现错误…你真的这么认为吗?请给我们一些输入和预期输出,当前输出不认为它实现错误(在我的代码中)但我一直在追查,这是我不确定的一件事。输入一个以“07”开头的电话号码应该可以阻止它被写入文件,并在ErrorDisplay中显示一条消息谢谢,这已经奏效了!!我只需要使用“MobileNumber.toString().startsWith”(“07”)因为我是在Eclipse中完成的,可以设置EditText的长度。谢谢!!