配列をループして各文字を数え戻す方法を研究しました。私はこれをascii値と比較することでこれを行なった。私が持っている問題は、大文字にすることはできません。大文字もカウントするように設定するにはどうすればよいですか?Java配列の大文字と小文字を区別する
package test;
import java.util.Arrays;
public class arrays {
public static void array(char[] charAlphabet) {
int intCount = 97;
int intNumberof = 0;
//sort the array into alphabetical order
Arrays.sort(charAlphabet);
//look at each character
for(int x = 0; x < charAlphabet.length;) {
//if the position of the character is equal to its place in the alphabet, increase the count
if(charAlphabet[x] == intCount) {
intNumberof++;
x++;
}
//when it reaches the end of the letters, print it
else {
intCount++;
//print the letter and the number of letters
System.out.println(Character.toString(charAlphabet[x -1]) + " x " + intNumberof);
intNumberof = 0;
}
}
}
//input
public static void main(String[] dfsgsdg) {
char[] charAlphabet = "aabcdefghijklmnoapqrstuvwxyaz".toCharArray();
array(charAlphabet);
}
}
--Edit--
はちょうど実際に私のロジックは、それが実際には配列の最後の文字をオフにミスということで、少しオフになっていることに気づいたが、私は印刷に-1削除した場合、それは実際に最初から逃げ出します。
大文字は32だけずれているため、オフセットを確認するだけで済みます。それはおそらく小文字に変換し、平等をチェックする方が簡単でしょう。 –