2013-10-28 48 views
0

我使用IF語句來檢查值與移動電話號碼07.這開始是我的代碼: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()正在執行wrong_ ...你真的這麼認爲嗎? –

+1

請給我們一些輸入和期望的輸出和當前的輸出 –

+0

沒想到這是執行錯誤(在我的代碼中),但追溯它,這是我不知道的一件事。輸入一個以「07」開頭的電話號碼應該停止寫入文件,並在ErrorDisplay中顯示一條消息 –

回答

2

此方法只需檢查手機號碼是否以07開頭,因爲XML指定號碼只能是號碼,長度只能爲11位。

我只想用:

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

} 
+0

謝謝,這已經奏效了!我只需要使用「MobileNumber.toString()。startsWith(」07「),因爲我在Eclipse中這樣做,並且可以設置EditText的長度。 –

0

if(num.startsWith("0") && num.startsWith("7", 1))if(num.startsWith("07"))會工作。