Java 无法让我的for循环从hashmap中为我提供最不常见的字母
我所有的东西都在工作,只是我不明白为什么我的代码找不到最不常用的字母。我有一个for循环,应该找到最不常用的字母,但它没有给我任何东西。它给了我最普通但不是最不重要的东西Java 无法让我的for循环从hashmap中为我提供最不常见的字母,java,hashmap,Java,Hashmap,我所有的东西都在工作,只是我不明白为什么我的代码找不到最不常用的字母。我有一个for循环,应该找到最不常用的字母,但它没有给我任何东西。它给了我最普通但不是最不重要的东西 FileReader file = new FileReader("\\src\\alphaCounter\\"); BufferedReader reader = new BufferedReader(file); HashMap<Character,Integer> myHashSet
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-leastcommon=0
与
int leastcommon=Integer.MAX\u值
理由
当LeastCommon初始化为0时,代码的以下条件永远不会满足:
ifmyHashSet.getcharalpha尝试使用上面的代码。在同一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("") ;
如何初始化LeastCommon?发布一个示例输入和预期,实际输出我的代码打印此消息出现最少的字母至少应该告诉我基于我的file@Lock13B如果你得到了问题的答案,请不要忘记通过接受回答问题的答案,将问题标记为已回答。
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