首页 > 解决方案 > 字典输入文件和字符串操作

问题描述

我正在编写一个程序,该程序搜索导入的文件以查找用户输入的字符串和长度。例如,“输入单词中可能的字母:”键盘扫描“aeppr”“输入目标单词中的字母数:”“5”

然后继续搜索我的字典文件并最终打印: 1 纸

我想知道您是否可以使用 indexOf 或任何其他方法或类来显示此结果。截至目前,我的代码仅显示与搜索的字母和长度完全匹配的单词。任何帮助或建议将不胜感激。

    String input;
    String altInput;

    Scanner inFile = new Scanner(new File("words.txt"));
    Scanner scanner = new Scanner(System.in);

    String lettersBeingTested;
    int numberOfLetters;

    System.out.println("Enter the possible letters in your word: ");
    lettersBeingTested = scanner.next();
    System.out.println("Enter the number of letters in your target words: ");
    numberOfLetters = scanner.nextInt();
    int count = 0;
    while (inFile.hasNext()) {

        input = inFile.next();
        altInput = "";

        for (int i = 0; i < input.length(); i++) {

            altInput = altInput + input.charAt(i);

            if (input.contains(lettersBeingTested) && altInput.length() == numberOfLetters) {

                count++;
                System.out.println(count + " " + altInput);

            }
        }
    }
    System.out.println("End of list: " + count + " words found");

    inFile.close();
}

标签: javastringint

解决方案


public static void main(String[] args) throws FileNotFoundException {
    findWords(new File("words.txt"));
}

public static void findWords(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(System.in)) {
        System.out.println("Enter the possible letters in your word: ");
        String lettersBeingTested = scan.next();
        System.out.println("Enter the number of letters in your target words: ");
        int numberOfLetters = scan.nextInt();
        int[] requiredHistogram = histogram(lettersBeingTested, new int[26]);

        Predicate<int[]> predicate = wordHistogram -> {
            for (int i = 0; i < requiredHistogram.length; i++)
                if (requiredHistogram[i] > 0 && wordHistogram[i] < requiredHistogram[i])
                    return false;
            return true;
        };

        Set<String> words = findWords(file, predicate, numberOfLetters);
        int i = 1;

        for (String word : words)
            System.out.println(i + " " + word);

        System.out.println("End of list: " + words.size() + " words found");
    }
}

private static int[] histogram(String str, int[] histogram) {
    Arrays.fill(histogram, 0);
    str = str.toLowerCase();

    for (int i = 0; i < str.length(); i++)
        histogram[str.charAt(i) - 'a']++;

    return histogram;
}

private static Set<String> findWords(File file, Predicate<int[]> predicate, int numberOfLetters) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        Set<String> words = new LinkedHashSet<>();
        int[] histogram = new int[26];

        while (scan.hasNext()) {
            String word = scan.next().toLowerCase();

            if (word.length() == numberOfLetters && predicate.test(histogram(word, histogram)))
                words.add(word);
        }

        return words;
    }
}

使用直方图看起来有点复杂。我认为 if lettersBeingTested = "aa",那么您正在寻找其中至少有2 个 'a' 的单词。因此,您必须构建直方图并比较当前单词和示例一中的符号出现次数。

附言

altInput = altInput + input.charAt(i);

循环中的字符串连接会导致性能不佳。看看StringBuilder不是。


推荐阅读