需要帮助根据用户输入列出字符的频率



当前代码 我列出了字母数组的频率,我想知道是否有办法合并用户可用的数字和所有标点符号。(即 ASCII 文本( 我感谢任何帮助!

import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int i = 0; 
int j = 0; 
int k = 0; 
String str;
char c, ch;

System.out.print("Enter a String : ");
str=scan.nextLine();
i=str.length();
for(c='A'; c<='z'; c++)
{
k=0;
for(j=0; j<i; j++)
{
ch = str.charAt(j);
if(ch == c)
{
k++;
}
}
if(k>0)
{
System.out.println( c + "  "  + k );
}

}
}
}
input
Enter a String : jhdvaevaecvieabvuae[;;;/'[298734327
output
[  2
a  4
b  1
c  1
d  1
e  4
h  1
i  1
j  1
u  1
v  4

此外,此代码已准备好适应大写和小写区分。

您可以使用以下步骤。 创建一个频率数组,其容量为 128(ASCII 字符集大小(。将所有频率元素初始化为 0。现在扫描输入字符串的每个字符,并在频率数组中将频率增加 1。数组的索引可以通过将当前字符转换为其整数表示来计算。为了您的参考,您可以浏览下面提供的代码。

public String computeFrequency(String input) {
int []frequecy = new int[128]; // each element of array represent frequency of some character indexed by character's ascii code
for(char ch: input.toCharArray()) {
int intCurrentChar = (int) ch;  // get ascii code of current character. It can be obtained by casting character to integer in java.
frequecy[intCurrentChar]++; // increase the frequency of current character
}
// collect all non zero frequency to string
StringBuilder sbr = new StringBuilder();
for(int frequencyIndex = 0; frequencyIndex <128; frequencyIndex++) {
if( frequecy[frequencyIndex]>0) {
char ch = (char) frequencyIndex; // get ascii character from ascii code. It can be obtained by casting integer to character in java.
sbr.append(ch).append(" ").append(System.lineSeparator());
}
}
return sbr.toString();  
}

使用 java 8,您可以通过streamgroupingBy轻松处理此问题,如下所示:

import static java.util.stream.Collectors.*;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String : ");
String str =scan.nextLine();
String ret = str.chars().mapToObj(c -> (char) c).collect(groupingBy(c -> c, counting()))
.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue))
.map(entry -> entry.getKey() + ": " + entry.getValue())
.collect(joining(", "));
System.out.println(ret);

这是演示(输入和输出(:

Enter a String : scan.nextLine();
a: 1, c: 1, s: 1, t: 1, (: 1, x: 1, ): 1, i: 1, ;: 1, L: 1, .: 1, e: 2, n: 3

这就是地图的用途。

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String : ");
String s = scan.nextLine();
Map<Character, Integer> frequency = new TreeMap<>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
Integer n = frequency.get(c);
if (n == null) frequency.put(c, 1);
else frequency.put(c, n + 1);
}
for (Map.Entry<Character, Integer> e : frequency.entrySet()) {
System.out.println(e.getKey() + " " + e.getValue());
}
}

不要忘记导入java.util.Mapjava.util.TreeMap

如果你想支持键盘上的所有字符,为什么要这样检查呢?(用户在技术上可以输入任何 ASCII 字符(。下面是一个更简单的解决方案,可以为您提供更好的时间性能

public static void main(String[] args) {
Map<Character, Integer> rst = new HashMap<>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
String str = sc.nextLine();
char[] charArr = str.toCharArray();
for(int i = 0; i < charArr.length; i++){
int cnt = rst.containsKey(charArr[i]) ? rst.get(charArr[i])+1 : 1;
rst.put(charArr[i], cnt);
}
rst.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " " + entry.getValue());
});
}

下面是一个运行示例:

Enter String: 
dfasrewrqe234342#$@#%@#$%@#$
@ 3
a 1
# 4
d 1
$ 3
e 2
% 2
f 1
q 1
r 2
2 2
s 1
3 2
4 2
w 1
Process finished with exit code 0

相关内容

  • 没有找到相关文章

最新更新