2016-04-29 3 views
-3

Это мой код:как я возвращать число вхождений буквы с

public int frequency (char c) 
{ 
    int numOccurrences = 0; 
    String ltr = "letters"; 
    ltr = ltr.toLowerCase(); 
    for (int i = 0; i < ltr.length(); i++) 
    { 
     if (l.charAt(0) == l.charAt(1)) 
     { 
      numOccurrences = numOccurrences + 1; 
     } 
    } 
    return numOccurrences; 
} 

public class TextAnalyser 
{ 
    private int letters;  // the total number of letters analysed 

    private int[] frequencies; // the number of occurrences of each letter of the alphabet (case-insensitive) 

    // initialise the instance variables 
    public TextAnalyser() 
    { 
     this.letters = 0; 
     this.frequencies = new int[26]; 
    } 

    public TextAnalyser(String initial) 
    { 
     this.letters = 0; 
     this.frequencies = new int [26]; 
     this.analyse(initial); 
    } 
} 
+0

Пожалуйста, отформатируйте код. – Akah

+0

Я не знаю, но я чувствую, что половина вашего кода отсутствует в вопросе. – SomeJavaGuy

+0

'if ((c) == l.charAt (i)) { numOccurrences = numOccurrences + 1; } ' – ashwinbhy

ответ

0

Если я в состоянии получить вас правильно, вы хотите считать появление символа с в строку? если да, то StringUtils имеет то, что вы можете использовать

int count = StringUtils.countMatches(CharSequence myString, CharSequence subString); 

это подсчитывает, сколько раз появляется подстрока в MyString. Дополнительная информация от Here

Смежные вопросы