不能让我的for循环给我一个来自我的hashmap的最不常见的字母

时间:2018-03-22 03:54:26

标签: java hashmap

我有一切正常工作,除了我无法弄清楚为什么我的代码不会找到最不常见的字母,我有一个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); 

3 个答案:

答案 0 :(得分:1)

<强>替换

int leastCommont = 0;

。通过

int leastCommont = Integer.MAX_VALUE;

<强>原因

leastCommont初始化为0时,永远不会满足以下代码条件:

if(myHashSet.get((char)alpha) < leastCommont)

答案 1 :(得分:0)

  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子句中获得了最高和最低。你需要以不同的方式处理它。从早期的代码来看,它甚至没有出现在最不常用的字符中。

答案 2 :(得分:0)

我希望这段代码可以帮到你。我做了一个简单的char计数器。您可以使用此功能创建所需的功能。如果你想要不区分大小写,请将输入字符串设置为大写或大写。

    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