有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的java错误:找不到符号方法

我试图编译下面的代码,但我不断得到错误

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

共 (2) 个答案

  1. # 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()是字符串对象的方法。所以你做str.toCharacterArray(),而不是toCharacterArray(str)

    对于第二个问题,在您向我们展示的代码中没有实现writeSuccess()方法

  2. # 2 楼答案

    你要做的是

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