如何从用户输入的字符串句子中获取最大出现字符及其出现次数?
How to get the maximum occurring character and its occurrences from a user inputted String sentence?
我有一个程序要求用户输入字符串句子并输出其最大出现字符及其出现次数。
我的问题是,计算最大字符及其出现次数的函数只计算单个单词(全部小写)的最大字符及其出现次数,而不是完整的句子或以开头的单词一个大写字母。
如果用户输入一个句子,程序一直在freqLetter数组(频繁字母数组)中有一个索引越界,我不知道为什么它会越界,它有什么关系吗与句子的空格?我应该在迭代数组时创建另一个循环还是应该创建另一个数组?? (我有时会在操作数组索引时感到困惑)。
代码:
static void maxOccuringChar(char str[]) { // function that counts that max character and its occurences
char maxChar = ' ';
int freqLetter[] = new int[26];
int lengthString = str.length;
int maximum = -1;
for (int i = 0; i < lengthString; i++)
freqLetter[str[i] - 'a']++; // I'm not sure why it becomes out of bounds for some reason
for (int i = 0; i < 26; i++)
if (maximum < freqLetter[i]) {
maximum = freqLetter[i];
maxChar = (char)(i + 'a');
}
System.out.print(maxChar + " = " + maximum); // returns result
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] StringInput = in.nextLine().toCharArray(); // user inputs the given String sentence and puts into character array
maxOccuringChar(StringInput); // calls function and returns the maximum character and and its occurences
}
Output 1:
Elephant
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -28 out of bounds
for length 26
Output 2:
I am confused
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -24 out of bounds
for length 26
Output 3: // works fine here
hello
l = 2
Process finished with exit code 0
非常感谢您的回复,在这方面确实对我有帮助!
非常感谢大家!!!
出现问题是因为 space 代码是 32。更改循环以跳过 spaces
for (int i = 0; i < lengthString; i++) {
if(str[i] == ' ') {
continue;
}
freqLetter[str[i] - 'a']++;
}
你也可以使用streams
解决这个问题
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println(input.chars()
.mapToObj(x -> (char) x)
.collect(Collectors.groupingBy(x -> x, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparingLong(Map.Entry::getValue))
.get());
}
输出:
123333
3=4
但它也会计算 spaces。如果您不想,则在 mapToObj(...)
之后添加此行
.filter(c -> c != ' ')
我有一个程序要求用户输入字符串句子并输出其最大出现字符及其出现次数。
我的问题是,计算最大字符及其出现次数的函数只计算单个单词(全部小写)的最大字符及其出现次数,而不是完整的句子或以开头的单词一个大写字母。
如果用户输入一个句子,程序一直在freqLetter数组(频繁字母数组)中有一个索引越界,我不知道为什么它会越界,它有什么关系吗与句子的空格?我应该在迭代数组时创建另一个循环还是应该创建另一个数组?? (我有时会在操作数组索引时感到困惑)。
代码:
static void maxOccuringChar(char str[]) { // function that counts that max character and its occurences
char maxChar = ' ';
int freqLetter[] = new int[26];
int lengthString = str.length;
int maximum = -1;
for (int i = 0; i < lengthString; i++)
freqLetter[str[i] - 'a']++; // I'm not sure why it becomes out of bounds for some reason
for (int i = 0; i < 26; i++)
if (maximum < freqLetter[i]) {
maximum = freqLetter[i];
maxChar = (char)(i + 'a');
}
System.out.print(maxChar + " = " + maximum); // returns result
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] StringInput = in.nextLine().toCharArray(); // user inputs the given String sentence and puts into character array
maxOccuringChar(StringInput); // calls function and returns the maximum character and and its occurences
}
Output 1:
Elephant
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -28 out of bounds
for length 26
Output 2:
I am confused
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -24 out of bounds
for length 26
Output 3: // works fine here
hello
l = 2
Process finished with exit code 0
非常感谢您的回复,在这方面确实对我有帮助!
非常感谢大家!!!
出现问题是因为 space 代码是 32。更改循环以跳过 spaces
for (int i = 0; i < lengthString; i++) {
if(str[i] == ' ') {
continue;
}
freqLetter[str[i] - 'a']++;
}
你也可以使用streams
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println(input.chars()
.mapToObj(x -> (char) x)
.collect(Collectors.groupingBy(x -> x, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparingLong(Map.Entry::getValue))
.get());
}
输出:
123333
3=4
但它也会计算 spaces。如果您不想,则在 mapToObj(...)
.filter(c -> c != ' ')