我有一切工作,除了我无法弄清楚为什么我的代码找不到最不常见的字母我有一个 for 循环,它应该找到最不常见的字母,但它没有给我任何东西。 它给了我最常见的,但并非最不重要的。
FileReader file = new FileReader("\src\alphaCounter\");
BufferedReader reader = new BufferedReader(file);
HashMap<Character,Integer> myHashSet = new HashMap<Character, Integer>();
myHashSet.put('a', 0);
/** this goes to Z
int mostCommon = 0;
char mostCommonLtr = ' ';
int leastCommont = 0;
char leastCommonLtr = ' ';
Object[] words = reader.lines().toArray();
/**
* this loop has changed all the letters to lower case
*/
for(Object word : words){
String wordString = word.toString();
wordString = wordString.toLowerCase();
/**
*
*/
if(wordString.length() > bigWord.length()){
bigWord = wordString;
}
for(int alpha = 0; alpha < wordString.length(); alpha++){
myHashSet.put(wordString.charAt(alpha), myHashSet.get(wordString.charAt(alpha)) + 1);
}
}
for(int alpha = 'a'; alpha<= 'z'; alpha++){
System.out.println("The number of " + (char)alpha + "'s in the words.txt = " + myHashSet.get((char)alpha ));
if(myHashSet.get((char)alpha) > mostCommon) {
mostCommonLtr = (char)alpha;
mostCommon = myHashSet.get((char)alpha);
/**
* this gave me the most common letter
*/
if(myHashSet.get((char)alpha) < leastCommont) {
leastCommonLtr = (char)alpha;
leastCommont = myHashSet.get((char)alpha);
/**
* this was supposed to give me the least common
*/
}
}
System.out.println("The letter that appeared the least is " + leastCommonLtr);
System.out.println("The letter that appears the most is " + mostCommonLtr);
替换
int leastCommont = 0;
跟
int leastCommont = Integer.MAX_VALUE;
原因
将leastCommont
初始化为 0 时,永远不会满足代码的以下条件:
if(myHashSet.get((char)alpha) < leastCommont)
if(myHashSet.get((char)alpha) != null && myHashSet.get((char)alpha) > mostCommon) {
mostCommonLtr = (char)alpha;
mostCommon = myHashSet.get((char)alpha);
/**
* this gave me the most common letter
*/
}
if(myHashSet.get((char)alpha) != null && myHashSet.get((char)alpha) > leastCommont && alpha != mostCommonLtr ) {
leastCommonLtr = (char)alpha;
leastCommont = myHashSet.get((char)alpha);
/**
* this was supposed to give me the least common
*/
}
尝试使用上面的代码段。您在相同的 if 子句中获得了最高和最低。您需要以不同的方式处理它。从早期的代码来看,它甚至没有使用最少的字符。
我希望这段代码能帮助你。我做了一个简单的字符计数器。您可以使用此功能使函数成为您想要的函数。如果要不区分大小写,请将输入字符串设置为小写或大写。
String wordString = "a1s3df2ad2fsa3dfwe3wrqasdf";
System.out.println("input string="+wordString) ;
char[] cword = wordString.toCharArray();
List<Character> mylist = new ArrayList<Character>();
Set<Character> charset = new HashSet<Character>();
HashMap<Character, Integer> myHashSet = new HashMap<Character, Integer>();
int maxcnt, mincnt;
for (char c : cword) {
mylist.add(c);
charset.add(c);
}
for (Character c : charset) {
int freq= Collections.frequency(mylist, c) ;
System.out.println("alpha " + c + " freq="+freq );
myHashSet.put(c, freq) ;
}
maxcnt = Collections.max(myHashSet.values()) ;
mincnt = Collections.min(myHashSet.values()) ;
System.out.println("maxcnt="+maxcnt+" mincnt="+mincnt) ;
// get max alpha
System.out.print("max count alphas: ") ;
for (Character c : charset ) {
if ( maxcnt == myHashSet.get(c).intValue() ) {
System.out.print(" "+c);
}
}
System.out.println("") ;
// get min alpha
System.out.print("min count alphas: ") ;
for (Character c : charset ) {
if ( mincnt == myHashSet.get(c).intValue() ) {
System.out.print(" "+c);
}
}
System.out.println("") ;
样本的输出
input string=a1s3df2ad2fsa3dfwe3wrqasdf
alpha a freq=4
alpha 1 freq=1
alpha q freq=1
alpha 2 freq=2
alpha r freq=1
alpha s freq=3
alpha 3 freq=3
alpha d freq=4
alpha e freq=1
alpha f freq=4
alpha w freq=2
maxcnt=4 mincnt=1
max count alphas: a d f
min count alphas: 1 q r e