首页 > 解决方案 > 递归将一个单词分成三个或更多单词java

问题描述

我正在尝试编写一个程序,该程序将从 dictionary.txt 文档中获取输入并将单词存储在字典列表中,然后确定该单词是否可以分解为三个或更多单词,如果可以,则打印出原始单词例如,后跟新单词的单词disconsolateness:disc on so lateness将是文档composedMore.txt 中的输出。现在,代码继续运行,但我没有得到任何输出,我不确定我做错了什么。您能提供的任何帮助将不胜感激。我在下面发布了我的代码,输入是字典中的任何单词。

import java.util.*;
import java.io.*;
public class CompositionTwo
{
    private static List<String> dictionary = new ArrayList<>();
    public static void main(String []args) { 
        File inputFile = new File("dictionary.txt");
        File outputFile = new File("composedMore.txt");
        Scanner in = null;
        PrintWriter out = null;

        try {
            in = new Scanner(inputFile);
            out = new PrintWriter(outputFile);
            while (in.hasNext()) {
                String input = in.next();
                dictionary.add(input);
                String output = splitWord(input, "");
                if (output != "") {
                    out.println(input + ":" + output);
                }
            }
        } catch (IOException e) {
            System.out.println("An IOException has occurred during output process.");
        } finally {
            in.close();
            out.close();
        }

    } 

    public static String splitWord(String word, String output) {

        if (word.length() == 0) {
            output = output;
        }
        else {
            for (int i = 1; i <= word.length(); i++) {
                // consider all prefixes of current String
                String prefix = word.substring(0, i);

                // if the prefix is present in the dictionary, add prefix to the
                // output String and recurse for remaining String

                if (dictionary.contains(prefix)) {
                    splitWord(word.substring(i), output + " " + prefix);
                }
            }
        }

        return output;
    }  
} 

标签: javarecursionio

解决方案


首先将所有单词添加到字典中,然后检查每个单词,因为您需要与文件中的所有单词进行比较您采用的过程只是比较了被比较单词之前的单词

import java.util.*;

 import java.io.*;

 public class CompositionTwo
 {

private static List<String> dictionary = new ArrayList<>();
public static void main(String []args) { 
    File inputFile = new File("dictionary.txt");
    File outputFile = new File("composedMore.txt");
    Scanner in = null;
    PrintWriter out = null;
    String word;

    try {
        in = new Scanner(inputFile);
        out = new PrintWriter(outputFile);
        //Read the file indictionary
        while (in.hasNext()) {
            String input = in.next();
            dictionary.add(input);
        }

        //Check All the words in dictionary for Splitting
        for(int i=0;i<dictionary.size();i++)
        {
            String output = splitWord(dictionary.get(i), "");
            if (!"".equals(output)) {
                String outa[] = output.split("\\s") ;
                if(outa.length >= 3) // Check if 3 words are created as output
                {
                System.out.println(dictionary.get(i) + ":" + output);
                out.println(dictionary.get(i) + ":" + output);
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        in.close();
        out.close();
    }

} 

public static String splitWord(String word, String output) {

    if (word.length() == 0) {
        return output;
    }
    else {
        for (int i = 1; i <= word.length(); i++) {
            // consider all prefixes of current String
            String prefix = word.substring(0, i);

            // if the prefix is present in the dictionary, add prefix to the
            // output String and recurse for remaining String

            if (dictionary.contains(prefix)) {
                return splitWord(word.substring(i), output + " " + prefix);
            }
        }
    }

    return output ;

}  

}


推荐阅读