String.charAt()未返回预期的值

时间:2013-10-28 10:38:40

标签: 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 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()实施错误。谢谢!

2 个答案:

答案 0 :(得分:2)

  

此方法只需检查手机号码是否以07开头,因为XML指定号码只能是号码,长度为11位。

我会用:

if(MobileNumber.toString().startsWith("07") && MobileNumber.length() <= 11){

}

答案 1 :(得分:0)

if(num.startsWith("0") && num.startsWith("7", 1)) 要么 if(num.startsWith("07"))可以使用。