首页 > 解决方案 > (Bug) 迭代列表后返回错误值

问题描述

这种方法的目的是,当我给它一个字符串作为输入时,它必须返回我最常出现的字母。

如果我给一个字符串作为输入,它可以正常工作:

String text = "helllo";

所以它给了我:

The most frequent letter is l with: 2 occurrences

这是正确的,但如果我把它作为输入

String text = "abbccdd";

它给了我

The most frequent letters are b,d with: 2 occurrences

这不正确,因为它必须给我

The most frequent letters are b,c,d with: 2 occurences

请帮忙!

业务逻辑.java

package com.mycompany.showcharwithhighestoccurrence.javafxBusinessLogic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BusinessLogic {


    public void hello() {
        System.out.println("helllllo");
    }

    public String giveFinalResult(String text) {

        String input = " ";
        String output = " ";
        if (text != null) {

            input = text.toLowerCase().replaceAll("\\s+", "");

            int[] freq = new int[input.length()];
            int i, j, max, lastFreq;
            lastFreq = 0;

            //Converts given string into character array
            char inputChars[] = input.toCharArray();

            List<Character> maxChars = new ArrayList<>();

            //Count each word in given string and store in array freq
            for (i = 0; i < inputChars.length; i++) {
                freq[i] = 1;
                for (j = i + 1; j < inputChars.length; j++) {
                    if (inputChars[i] == inputChars[j] && inputChars[i] != ' ' && inputChars[i] != '0') {
                        freq[i]++;
                        inputChars[j] = '0';
                    }
                }
            }

            //Determine maximum occurring characters
            if (freq.length > 0) {
                lastFreq = freq[0];
            }
            for (i = 0; i < freq.length; i++) {

                max = freq[0];
                if (freq[i] == lastFreq) {
                    max = lastFreq;
                }
                if (max == lastFreq && max < freq[i]) {
                    lastFreq = freq[i];
                    maxChars.add(inputChars[i]);
                    for (Character c : maxChars) {
                        if (c != null) {
                            output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
                        }
                    }
                }
                if (lastFreq < freq[i]) {
                    maxChars.clear();
                    maxChars.add(inputChars[i]);
                    for (Character c : maxChars) {
                        if (c != null) {
                            output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
                        }
                    }
                }

                if (max > 1 && max == freq[i]) {
                    maxChars.add(inputChars[i]);
                        output = "The most frequent letter are " + maxChars.get(0).toString() + ", "
                                + iterator.next().toString() + " with: " + freq[i] + "occurrences";
                    }
                }
            }
        }

        return output;
    }
}

标签: javaloopsforeachiterator

解决方案


为什么这么复杂?

您只需要 3 个简单的步骤:

    // collect frequencies
    Map<String, Long> collect = "abbccdde".chars()
            .mapToObj(c -> Character.toString((char) c))
            .collect(Collectors.groupingBy(x -> x, Collectors.counting()));

    // find max
    long max = collect.values().stream().max(Comparator.naturalOrder()).get();

    //filter values for max
    String result = collect.entrySet()
            .stream()
            .filter(x -> x.getValue() == max)
            .map(Map.Entry::getKey)
            .collect(Collectors.joining(","));

    String output = "The most frequent letter are " + result +  " with: " + max + "occurrences";

    System.out.println("output = " + output);

推荐阅读