2013-10-28 1 views
0

내가 값의 휴대폰 번호가 07이 시작 있는지 확인하는 IF 문을 사용하고 내 코드도 예상 :하면 String.charAt()가 반환하지 않습니다 값은

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()이 잘못 구현되었습니다. 감사!!

+0

_string.CharAt()가 잘못 구현되었습니다. ... 정말 그렇게 생각합니까? –

+1

약간의 입력과 예상 출력 및 전류 출력을주세요. –

+0

잘못 구현되었다고 생각하지 않았지만 통과하지 못했다고 생각했습니다. 그 점은 확실하지 않았습니다. "07"이외의 문자로 시작하는 전화 번호를 입력하면 파일에 기록되는 것을 멈추고 ErrorDisplay에 메시지를 표시해야합니다. –

답변

2

이 방법은 휴대 전화 번호가 07로 시작하는지 확인해야합니다. XML에서는 번호가 숫자이고 길이는 11 자리까지만 지정할 수 있기 때문입니다.

난 그냥 사용합니다 :

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

} 
+0

감사합니다. 내가 Eclipse에서 그것을하고 있기 때문에 "MobileNumber.toString(). startsWith ("07 ") 만 사용해야하고 EditText의 길이를 설정할 수 있습니다. 감사합니다 !! –

0

if(num.startsWith("0") && num.startsWith("7", 1)) 또는 if(num.startsWith("07"))이 작동합니다.