2016-09-08 3 views
0

私のコードは文字列を取り、アルファベットの各文字が何回文字列に現れるかを数え、その数のパーセンテージ情報で配列のヒストグラムを表示しますキャラクタが文字列内で発生した回数。文字列内の文字パーセントの出現の計算が正しくありません(java)

私のコードで、文字列に含まれる各文字の正しい割合が表示されないという問題がありましたが、それは数パーセントポイントで外れています。

たとえば、「hello world」と入力すると、Lは文字列の30%を構成する必要があります。しかし、私のコードはそれが文字列の27%を占めていると言っています。コードの問題か計算の問題かどうかはわかりません。

public class LetterCounter { 
    private static int[] alphabetArray; 
    private static char ch; 
    double stringLength; 



    /** 
    * Creates an array to hold the total number of occurences for each character of the alphabet 
    */ 
    public LetterCounter() 
    { 
     alphabetArray = new int[26]; 
    } 

    /** 
    * The method will go through the user inputted string, incrementing characters one by one 
    * when it comes across them in the string. 
    * @param input: the string that the user wants 
    */ 
    public void countLetters(String input) { 
     String userInput= input; 
     String s2 = userInput.toLowerCase(); 
     for (int i = 0; i < s2.length(); i++) { 
      char ch= s2.charAt(i); 
      if (ch >= 97 && ch <= 122){ 
       alphabetArray[ch-'a']++; 
      } 
     } 
     stringLength = s2.length(); 
    } 

    /** 
    * Calculates how many characters there are in the inputted string. Multiple strings that are input by the user will be 
    * added to the total character count. 
    * @return: the sum of how many characters there are in each string 
    */ 
    public int getTotalCount() { 
     int sum=0; 
     for (int i = 0; i < alphabetArray.length; i++) { 
      if(alphabetArray[i]>=0){ 
       sum = 0; 
       char ch = (char) (i+97); 
       for(int total : alphabetArray) { 
        sum += total; 
       }   
      } 

     } 
     return sum; 
    } 

    /** 
    * This method will reset the character count to zero for every character. 
    */ 
    public void reset() { 
     for (int i : alphabetArray) { 
      if(alphabetArray[i]>=0){ 
       alphabetArray[i]=0; 
       char ch = (char) (i+97); 
       System.out.println(ch +" : "+alphabetArray[i]); 
      }  
     } 
    } 

    /** 
    * The method prints out a histogram of the entire array, displaying the most commonly occuring letter as having 60 #s, 
    * with the rest of the letter's #s to the 60 #s. The percentage occurence of each character in the string is also displayed beside the character. 
    * @return: the histogram of the array 
    */ 
    public String toString() { 
     int max = alphabetArray[0]; 
     int markCounter = 0; 
     double percent = 0.0; 
     String s = ""; 

     for(int i =0; i<alphabetArray.length; i++) { 
      //finds the largest number of occurences for any letter in the string 
      if(alphabetArray[i] > max) { 
       max = alphabetArray[i]; 
      } 
     } 

     for (int i = 0; i < alphabetArray.length; i++) { 
      //percent = (alphabetArray[i] /stringLength) * 100; 
      percent = Math.round((alphabetArray[i] /stringLength) * 100); 
      markCounter = (alphabetArray[i] * 60)/max; 
      if(alphabetArray[i]>=0){ 
       char ch = (char) (i+97); 
       System.out.print(ch +" : "); 
      } 

      for (int hash =0; hash <markCounter; hash++) { 
       System.out.print("#"); 

      } 

      if(alphabetArray[i] >= 0){ 
        System.out.print(" " + percent + "%"); //+ percent); 

      } 
      System.out.println(); 

     } 

     return s; 
    } 
} 
+1

'3/11 = 0.272727 ...'または27%を使用することができます。どうした?あなたはスペースを数えることを忘れましたか?または、あなたのプログラムは、それが想定されていなかったときのスペースを数えましたか? –

+0

問題は、文字列内の文字を数えているだけです。スペースは含まれていません。 –

+0

ああ待っています。なぜ起こっているのか、ありがとう! –

答えて

1

string.length()関数を使用して文字列の長さを計算しています。この関数は文字列の長さも計算します。私はあなたが文字列で利用可能な総文字の割合を計算しようとしていると思います。

ループ内の文字列の長さを数え、各文字の頻度をチェックする必要があります。

public void countLetters(String input) { 
    String userInput= input; 
    String s2 = userInput.toLowerCase(); 
    for (int i = 0; i < s2.length(); i++) { 
     char ch= s2.charAt(i); 
     if (ch >= 97 && ch <= 122){ 
      alphabetArray[ch-'a']++; 
      stringLength++; 
     } 
    } 

} 

このようにすると、文字列内のcharの総数が取得されます。

また、アルファベット以外の文字を含めるかどうかを確認することもできます。特殊文字は文字列の一部でもかまいません。要件に応じて、stringlengthカウンタが更新されます。

0

String input = EditTextinput.getText()。toString(); input = input.replace( ""、 "");

場合によっては、文字列の先頭または末尾のスペースのみを削除することもできます(中央のものではなく)。その場合、トリム:

input = input.trim(); 
関連する問題