2017-10-20 28 views
0

配列をループして各文字を数え戻す方法を研究しました。私はこれを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削除した場合、それは実際に最初から逃げ出します。

+0

大文字は32だけずれているため、オフセットを確認するだけで済みます。それはおそらく小文字に変換し、平等をチェックする方が簡単でしょう。 –

答えて

0

「ABa」を試してみると、「a」(97)が2回表示されませんintNumberof++

"a22c"と同じです。

ソートを忘れて、すべての手紙をcharAlphabet[x].isUpperCase() respにチェックしてください。 isLowerCase()

+0

実際には私のロジックが少しオフになっていることに気付きました。実際には配列の最後の文字を逃してしまいますが、プリントで-1を取り除くと実際には最初から消えてしまいます。 – Marriott81

+0

はい、それを行う方法を考え直してください:より論理的な、短い名前、チェックする必要があるもの。 –

0

各文字を文字列として取り、このロジックを適用することができます。

String temp= str; 
If(str.toUpperCase().equals(temp)){ 
    //It is uppercase 
} 
else{ 
//lower case 
} 
関連する問題