Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算字符串中字符频率的最简单方法是什么?_Java - Fatal编程技术网

Java 计算字符串中字符频率的最简单方法是什么?

Java 计算字符串中字符频率的最简单方法是什么?,java,Java,我想计算字母表中的每个字符在字符串中出现的次数 这就是我到目前为止所做的: Scanner input = new Scanner(System.in); PrintStream out = System.out; out.print("Enter the string : "); String na = input.nextLine(); String n = na.toUpperCase(); i

我想计算字母表中的每个字符在字符串中出现的次数

这就是我到目前为止所做的:

        Scanner input = new Scanner(System.in);
        PrintStream out = System.out;
        out.print("Enter the string : ");
        String na = input.nextLine();
        String n = na.toUpperCase();
        int l = n.length() ;
        int len = l;
        int a1 =0 , b =0, c =0, d=0 , e =0 ,f=0 ,g =0 ,h =0, u=0 ,i1 =0,j=0 ,k=0 ,l1=0 ,m=0, n1=0, o=0, p=0, q=0,r=0,s=0, t1=0 , v=0, w=0, x=0, y=0, z  = 0;


         for (int i = 0; i< len ; i++)
         {
             char a = n.charAt(i);

             if(n.charAt(i) =='A')
             {
                a1++;
              }
             else if(n.charAt(i) =='B')
            {
                    b++;
             }
             else  if(n.charAt(i) =='C')
             {
                c++;
              }
             else if(n.charAt(i) =='D')
             {
                d++;
              }
             else if(n.charAt(i) =='E')
             {
                e++;
              }
             else  if(n.charAt(i) =='F')
             {
                f++;
              }
             else if(n.charAt(i) =='G')
                 {
                    g++;
                  }
                else if(n.charAt(i) =='H')
                    {
                        h++;
                     }
                    else if(n.charAt(i) =='I')
                     {
                            i1++;
                       }
                    else if(n.charAt(i) =='J')
                      {
                        j++;
                       }
                     else if(n.charAt(i) =='K')
                     {
                        k++;
                     }
                     else   if(n.charAt(i) =='L')
                     {
                        l++;
                      }
                     else if(n.charAt(i) =='M')
                     {
                        m++;
                      }
                     else   if(n.charAt(i) =='N')
                     {
                        n1++;
                      }
                     else    if(n.charAt(i) =='O')
                     {
                        o++;
                      }
                     else  if(n.charAt(i) =='P')
                     {
                        p++;
                      }
                     else if(n.charAt(i) =='Q')
                     {
                    q++;
                     }
                     else   if(n.charAt(i) =='R')
                     {
                        r++;
                     }

         }
    out.print(a1+"A" +b+"B"+c+"C"+d+"D"+e+"E"+f+"F"+g+"G"+h+"H"+i1+"I"+j+"J"+k+"K"+l1+"L"+m+"M"+n1+"N"+o+"O"+p+"P"+q+"Q"+r+"R");
    }

}
扫描仪输入=新扫描仪(System.in);
PrintStream out=System.out;
out.print(“输入字符串:”);
字符串na=input.nextLine();
字符串n=na.toUpperCase();
int l=n.长度();
int len=l;
INTA1=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,u=0,i1=0,j=0,k=0,l1=0,m=0,n1=0,o=0,p=0,q=0,r=0,s=0,t1=0,v=0,w=0,x=0,y=0,z=0;
对于(int i=0;i
使用实现
Map
接口的类。让每个
Char
映射到一个
整数
,该整数表示
Char
出现的时间。迭代字符串,增加字符映射中的计数。

您可能可以使用映射接口,执行如下操作:

Map<Character,Integer> map = new HashMap<Character,Integer>();
for (int i = 0; i < s.length(); i++) {
  char ch = s.charAt(i);
  if (map.containsKey(ch)) {
    int cnt = map.get(ch);
    map.put(ch, ++cnt);
  } else {
    map.put(ch, 1);
  }
}
Map Map=newhashmap();
对于(int i=0;i

这将计算所有字符并给您计数。

如果我理解您的问题,那么您可以使用
int
数组来存储计数。然后根据字符值计算正确的位置。差不多

System.out.println("Enter the string : ");
String line = scanner.nextLine();
int[] alphabet = new int['z' - 'a'];
for (char ch : line.toLowerCase().toCharArray()) {
    alphabet[ch - 'a']++;
}
for (int i = 0; i < alphabet.length; i++) {
    if (i != 0) System.out.print(", ");
    System.out.printf("'%c' = %d",
            Character.toUpperCase('a' + i), alphabet[i]);
}
System.out.println(“输入字符串:”);
字符串行=scanner.nextLine();
int[]字母表=新的int['z'-'a'];
for(char ch:line.toLowerCase().toCharArray()){
字母表[ch-'a']++;
}
for(int i=0;i
String na=“字母的混合体”;
char小写='a';
字符大写='A';
对于(int i=0;i<26;i++){
int count_of_tagert_lower=0;
上界的整数计数=0;
用于(字符串str:na.split(“”){
if(str.contains(String.valueOf(小写))){
塔格特低阶++的计数;
}
else if(str.contains(String.valueOf(大写))){
上++的计数;
}否则{
//以防万一?
}
}
System.out.println(String.valueOf(小写)+“Occursed”+tagert\u lower的计数);
System.out.println(String.valueOf(大写)+“Occursed”+tagert\u upper的计数);
大写++;
小写++;
}
}
    String na = "A MixXx of LeTTers";
    char lowercase = 'a';
    char uppercase = 'A';

    for(int i=0; i < 26; i++){

        int count_of_tagert_lower = 0;
        int count_of_tagert_upper = 0;
        for(String str : na.split("")){
            if (str.contains(String.valueOf(lowercase))){
                count_of_tagert_lower++;

            }
            else if (str.contains(String.valueOf(uppercase))){
                count_of_tagert_upper++;

            }else{
                //just in case?

            }


        }
        System.out.println(String.valueOf(lowercase) + " occured " + count_of_tagert_lower);
        System.out.println(String.valueOf(uppercase) + " occured " + count_of_tagert_upper);
        uppercase++;
        lowercase++;

    }

}