2015-04-20 21 views
0

我想編譯下面的代碼,但我不斷收到錯誤。Android錯誤:無法找到符號方法

Cannot find symbol method toCharacterArray(string)
Cannot find symbol method writeSuccess(int,char[],char[])

public class ControlFlow { 

    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; 

    public void start(){ 
     char[] sentenceToTest = toCharacterArray("the quick red fox jumps over the lazy brown dog"); 
     char[] missingLetters = new char[26]; 

     int numOfMissingLetters = 0; 

     for(int i=0; i < alphabet.length; i++){ 
      char letterToFind = alphabet[i]; 

      if(hasLetter(letterToFind, sentenceToTest)){ 
       missingLetters[numOfMissingLetters] = letterToFind; 
       numOfMissingLetters++; 
      } 
     } 

     writeSuccess(numOfMissingLetters,missingLetters,sentenceToTest); 
    } 

    public boolean hasLetter(char aLetter, char[] aSentence) { 
     boolean found = false; 
     int position = 0; 
     while(!found){ 
      if(aLetter == aSentence[position]){ 
       found = true; 
      }else if(position == aSentence.length - 1){ 
       break; 
      }else{ 
       position++; 
      } 
     } 
     return found; 
    } 
} 

回答

1

啥子,你所要做的就是

String abc="the quick red fox jumps over the lazy brown dog"; 

char[] sentenceToTest=abc.toCharArray(); 

,你不要在你的類中定義的方法writeSuccess

public void writeSuccess (int numOfMissingLetters,char[] missingLetters, char[] sentenceToTest){ 



    Log.e("","number of missing letters are : "+numOfMissingLetters); 
    Log.e("","------------------"); 

    for(int i=0 ;i<sentenceToTest.length();i++){ 

     Log.e("","sentence to test is : "+sentenceToTest[i]); 

    } 

    Log.e("","------------------"); 


    for(int i=0 ;i<missingLetters.length();i++){ 

     Log.e("","missing letter is : "+missingLetters[i]); 

    } 


} 
+0

真棒,修復了那個錯誤,謝謝,但現在我只是得到了writeuccess錯誤 –

+0

您沒有回答問題的第二部分。另外,你能解釋你的答案嗎? –

+0

@BradHazelnut你還沒有在你的課堂上定義你的方法 –

1
char[] sentenceToTest = toCharacterArray("the quick red fox jumps over the lazy brown dog"); 

應該是:

char[] sentenceToTest = "the quick red fox jumps over the lazy brown dog".toCharacterArray(); 

.toCharacterArray()是String對象的方法。所以你做str.toCharacterArray(),而不是toCharacterArray(str)

對於第二個問題,您沒有在您向我們展示的代碼中實施的writeSuccess()方法。

+0

哇,謝謝,這很有道理,非常感謝你的解釋,以及 –

+0

@BradHazelnut很高興我能幫忙!我不確定第二個問題,代碼不包括在什麼內容中你向我們展示了,如果你展示給我,我可以幫助你更多,我懷疑有一個參數不匹配。 –