This question already has an answer here:
这个问题在这里已有答案:
- Java program to find the character that appears the most number of times in a String? 8 answers
Java程序找到在String中出现次数最多的字符? 8个答案
I got a task from my university today:
我今天从大学得到了一个任务:
Write a program that reads a ( short ) text from the user and prints the so called max letter (most common character in string) , that the letter which the greatest number of occurrences of the given text . Here it is enough to look at English letters (A- Z) , and not differentiate between uppercase and lowercase letters in the count of the number of occurrences .
编写一个程序,从用户读取(短)文本并打印所谓的最大字母(字符串中最常见的字符),即给定文本出现次数最多的字母。这里足以查看英文字母(A-Z),而不是区分出现次数的大写和小写字母。
For example, if : text = " Ada bada " so should the print show the most common character, this example it would be a.
例如,如果:text =“Ada bada”,那么打印应该显示最常见的字符,这个例子就是a。
This is an introductory course, so in this submission we do not need to use the " scanner - class" . We have not gone through this so much. The program will use the show message input two get the text from user .
这是一个入门课程,因此在本次提交中我们不需要使用“scanner - 类”。我们没有经历过这么多。该程序将使用show message输入两个来自用户的文本。
Info: The program shall not use while loop ( true / false ) , "return " statement / "break " statement .
信息:程序不应使用while循环(true / false),“return”语句/“break”语句。
I've been struggling with how I can get char values into a table.. am I correct I need to use array to search for most common character? I think I need to use the binarySearch, but that only supports int not char.
我一直在努力学习如何将char值放入表中。我是否正确我需要使用数组来搜索最常见的字符?我想我需要使用binarySearch,但是它只支持int而不是char。
I'll be happy for any answers. hint's and solutions. etc.. if you're very kind a full working program, but again please don't use the things I have written down in the "info" section above.
我会很高兴得到任何答案。提示和解决方案。等等。如果你是一个非常善良的完整工作程序,但请再次请不要使用我在上面的“信息”部分写下的内容。
My code:
String text = showInputDialog("Write a short text: ");
//format string to char
String a = text;
char c = a.charAt(4);
/*with this layout it collects number 4 character in the text and print out.
* I could as always go with many char c... but that wouldn't be a clean program * code.. I think I need to make it into a for-loop.. I have only worked with * *for-loops with numbers, not char (letters).. Help? :)
*/
out.print( text + "\n" + c)
//each letter into 1 char, into table
//search for most used letter
3 个解决方案
#1
1
Here's the common logic:
这是常见的逻辑:
- split your string into chars
- loop over the chars
- store the occurrences in a hash, putting the letter as key and occurrences as value
- return the highest value in the hash
将你的字符串分成字符
在字符上循环
将事件存储在哈希中,将字母作为键,将出现值作为值
返回哈希值中的最高值
As how to split string into chars, etc., you can use Google. :)
至于如何将字符串拆分为字符等,您可以使用Google。 :)
这是一个类似的问题。
#2
0
There's a common program asked to write in schools to calculate the frequency of a letter in a given String. The only thing you gotta do here is find which letter has the maximum frequency. Here's a code that illustrates it:
有一个共同的程序被要求在学校写,以计算给定字符串中的字母的频率。你要做的唯一事情是找到哪个字母的最大频率。这是一个说明它的代码:
String s <--- value entered by user
char max_alpha=' '; int max_freq=0, ct=0;
char c;
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
for(int j=0;j<s.length();j++){
if(s.charAt(j)==c)
ct++;
} //for j
}
if(ct>max_freq){
max_freq=ct;
max_alpha=c;
}
ct=0;
s=s.replace(c,'*');
}
System.out.println("Letter appearing maximum times is "+max_alpha);
System.out.println(max_alpha+" appears "+max_freq+" times");
NOTE: This program presumes that all characters in the string are in the same case, i.e., uppercase or lowercase. You can convert the string to a particular case just after getting the input.
注意:此程序假定字符串中的所有字符都是相同的大小写,即大写或小写。获取输入后,您可以将字符串转换为特定的大小写。
#3
0
I guess this is not a good assigment, if you are unsure about how to start. I wish you for having better teachers!
如果你不确定如何开始,我想这不是一个好的分配。我希望你有更好的老师!
So you have a text, as:
所以你有一个文本,如:
String text = showInputDialog("Write a short text: ");
The next thing is to have a loop which goes trough each letter of this text, and gets each char of it:
接下来的事情是有一个循环,它通过这个文本的每个字母,并获取它的每个字符:
for (int i=0;i<text.length();i++) {
char c=text.charAt(i);
}
Then comes the calculation. The easiest thing is to use a hashMap. I am unsure if this is a good topic for a beginners course, so I guess a more beginner friendly solution would be a better fit.
然后是计算。最简单的方法是使用hashMap。我不确定这对于初学者课程是否是一个好主题,所以我想一个更适合初学者的解决方案会更合适。
Make an array of integers - this is the "table" you are referring to. Each item in the array will correspond to the occurrance of one letter, e.g. histogram[0] will count how many "A", histogram[1] will count how many "B" you have found.
创建一个整数数组 - 这是你所指的“表”。数组中的每个项目将对应于一个字母的出现,例如直方图[0]将计算“A”的数量,直方图[1]将计算您找到的“B”的数量。
int[] histogram = new int[26]; // assume English alphabet only
for (int i=0;i<histogram.length;i++) {
histogram[i]=0;
}
for (int i=0;i<text.length();i++) {
char c=Character.toUppercase(text.charAt(i));
if ((c>=65) && (c<=90)) {
// it is a letter, histogram[0] contains occurrences of "A", etc.
histogram[c-65]=histogram[c-65]+1;
}
}
Then finally find the biggest occurrence with a for loop...
然后终于找到一个for循环最大的事件......
int candidate=0;
int max=0;
for (int i=0;i<histogram.length;i++) {
if (histogram[i]>max) {
// this has higher occurrence than our previous candidate
max=histogram[i];
candidate=i; // this is the index of char, i.e. 0 if A has the max occurrence
}
}
And print the result:
并打印结果:
System.out.println(Character.toString((char)(candidate+65));
Note how messy this all comes as we use ASCII codes, and only letters... Not to mention that this solution does not work at all for non-English texts.
请注意这一切是多么混乱,因为我们使用ASCII码,只有字母......更不用说这个解决方案根本不适用于非英语文本。
If you have the power of generics and hashmaps, and know some more string functions, this mess can be simplified as:
如果您具有泛型和散列图的强大功能,并且知道更多的字符串函数,那么这个混乱可以简化为:
String text = showInputDialog("Write a short text: ");
Map<Char,Integer> histogram=new HashMap<Char,Integer>();
for (int i=0;i<text.length();i++) {
char c=text.toUppercase().charAt(i));
if (histogram.containsKey(c)) {
// we know this letter, increment its occurrence
int occurrence=histogram.get(c);
histogram.put(c,occurrence+1);
}
else {
// we dunno this letter yet, it is the first occurrence
histogram.put(c,1);
}
}
char candidate=' ';
int max=0;
for (Char c:histogram.keySet()) {
if (histogram.get(c)>max) {
// this has higher occurrence than our previous candidate
max=histogram.get(c);
candidate=c; // this is the char itself
}
}
System.out.println(c);
small print: i didn't run this code but it shall be ok.
小字体:我没有运行此代码,但它应该没问题。
#1
1
Here's the common logic:
这是常见的逻辑:
- split your string into chars
- loop over the chars
- store the occurrences in a hash, putting the letter as key and occurrences as value
- return the highest value in the hash
将你的字符串分成字符
在字符上循环
将事件存储在哈希中,将字母作为键,将出现值作为值
返回哈希值中的最高值
As how to split string into chars, etc., you can use Google. :)
至于如何将字符串拆分为字符等,您可以使用Google。 :)
这是一个类似的问题。
#2
0
There's a common program asked to write in schools to calculate the frequency of a letter in a given String. The only thing you gotta do here is find which letter has the maximum frequency. Here's a code that illustrates it:
有一个共同的程序被要求在学校写,以计算给定字符串中的字母的频率。你要做的唯一事情是找到哪个字母的最大频率。这是一个说明它的代码:
String s <--- value entered by user
char max_alpha=' '; int max_freq=0, ct=0;
char c;
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
for(int j=0;j<s.length();j++){
if(s.charAt(j)==c)
ct++;
} //for j
}
if(ct>max_freq){
max_freq=ct;
max_alpha=c;
}
ct=0;
s=s.replace(c,'*');
}
System.out.println("Letter appearing maximum times is "+max_alpha);
System.out.println(max_alpha+" appears "+max_freq+" times");
NOTE: This program presumes that all characters in the string are in the same case, i.e., uppercase or lowercase. You can convert the string to a particular case just after getting the input.
注意:此程序假定字符串中的所有字符都是相同的大小写,即大写或小写。获取输入后,您可以将字符串转换为特定的大小写。
#3
0
I guess this is not a good assigment, if you are unsure about how to start. I wish you for having better teachers!
如果你不确定如何开始,我想这不是一个好的分配。我希望你有更好的老师!
So you have a text, as:
所以你有一个文本,如:
String text = showInputDialog("Write a short text: ");
The next thing is to have a loop which goes trough each letter of this text, and gets each char of it:
接下来的事情是有一个循环,它通过这个文本的每个字母,并获取它的每个字符:
for (int i=0;i<text.length();i++) {
char c=text.charAt(i);
}
Then comes the calculation. The easiest thing is to use a hashMap. I am unsure if this is a good topic for a beginners course, so I guess a more beginner friendly solution would be a better fit.
然后是计算。最简单的方法是使用hashMap。我不确定这对于初学者课程是否是一个好主题,所以我想一个更适合初学者的解决方案会更合适。
Make an array of integers - this is the "table" you are referring to. Each item in the array will correspond to the occurrance of one letter, e.g. histogram[0] will count how many "A", histogram[1] will count how many "B" you have found.
创建一个整数数组 - 这是你所指的“表”。数组中的每个项目将对应于一个字母的出现,例如直方图[0]将计算“A”的数量,直方图[1]将计算您找到的“B”的数量。
int[] histogram = new int[26]; // assume English alphabet only
for (int i=0;i<histogram.length;i++) {
histogram[i]=0;
}
for (int i=0;i<text.length();i++) {
char c=Character.toUppercase(text.charAt(i));
if ((c>=65) && (c<=90)) {
// it is a letter, histogram[0] contains occurrences of "A", etc.
histogram[c-65]=histogram[c-65]+1;
}
}
Then finally find the biggest occurrence with a for loop...
然后终于找到一个for循环最大的事件......
int candidate=0;
int max=0;
for (int i=0;i<histogram.length;i++) {
if (histogram[i]>max) {
// this has higher occurrence than our previous candidate
max=histogram[i];
candidate=i; // this is the index of char, i.e. 0 if A has the max occurrence
}
}
And print the result:
并打印结果:
System.out.println(Character.toString((char)(candidate+65));
Note how messy this all comes as we use ASCII codes, and only letters... Not to mention that this solution does not work at all for non-English texts.
请注意这一切是多么混乱,因为我们使用ASCII码,只有字母......更不用说这个解决方案根本不适用于非英语文本。
If you have the power of generics and hashmaps, and know some more string functions, this mess can be simplified as:
如果您具有泛型和散列图的强大功能,并且知道更多的字符串函数,那么这个混乱可以简化为:
String text = showInputDialog("Write a short text: ");
Map<Char,Integer> histogram=new HashMap<Char,Integer>();
for (int i=0;i<text.length();i++) {
char c=text.toUppercase().charAt(i));
if (histogram.containsKey(c)) {
// we know this letter, increment its occurrence
int occurrence=histogram.get(c);
histogram.put(c,occurrence+1);
}
else {
// we dunno this letter yet, it is the first occurrence
histogram.put(c,1);
}
}
char candidate=' ';
int max=0;
for (Char c:histogram.keySet()) {
if (histogram.get(c)>max) {
// this has higher occurrence than our previous candidate
max=histogram.get(c);
candidate=c; // this is the char itself
}
}
System.out.println(c);
small print: i didn't run this code but it shall be ok.
小字体:我没有运行此代码,但它应该没问题。