문제

I am using an IF statement to check if the value MobileNumber begins with 07. This is my code:

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);

    }

Can you tell me what i am doing wrong?? I assume either my logic operators && and || are wrong, or the string.CharAt() is being implemented wrong. Thank!!

도움이 되었습니까?

해결책

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.

I would just use:

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

}

다른 팁

if(num.startsWith("0") && num.startsWith("7", 1)) or if(num.startsWith("07")) will work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top