递归将一个单词分成三个或更多单词java

时间:2019-04-25 00:08:32

标签: java recursion io

我正在尝试编写一个程序,该程序将接收来自dictionary.txt文档的输入并将这些单词存储在词典列表中,然后确定该单词是否可以分为三个或更多单词,如果可以,则进行打印原来的单词后面跟着新单词,例如disconsolateness:disc on so lateness将是组成More.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;
    }  
} 

2 个答案:

答案 0 :(得分:0)

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

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 ;

}  

}

答案 1 :(得分:0)

假设您有这本字典:

disc
on
so
lateness

并假设您针对字符串disconsolateness运行程序。 我进行了一些更改,首先加载字典,然后调用递归方法splitWord

您应该知道,递归方法被称为将全部放在堆栈中,然后逐个返回一个调用。因此,我选择将递归调用更改为:

return prefix + " " + splitWord(word.substring(i), output);

以便在每个方法调用之后将找到的每个单词连接起来。

很显然,就像您已经做过的那样,当变量output中没有更多字符时,我的停止条件仍然存在。

    package io.gdfb.questions;

    import java.util.*;
    import java.io.*;

    public class CompositionTwo {
        private static List<String> dictionary = new ArrayList<>();

        public static void main(String[] args) {
            loadDictionary();

            File outputFile = new File("composedMore.txt");
            PrintWriter out = null;
            try {
                out = new PrintWriter(outputFile);
                String output = splitWord(args[0], "");
                if (output != "") {
                    out.println(args[0] + ":" + output);
                }
            } catch (IOException e) {
                System.out.println("An IOException has occurred during output process.");
            } finally {
                out.flush();
                out.close();
            }

        }

        private static void loadDictionary() {
            InputStream inputFile = Thread.currentThread().getContextClassLoader().getResourceAsStream("dictionary.txt");
            Scanner in = null;
            try {
                in = new Scanner(inputFile);
                while (in.hasNext()) {
                    String input = in.next();
                    dictionary.add(input);
                }
            } finally {
                in.close();
            }
        }

        public static String splitWord(String word, String output) {
            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 prefix + " " + splitWord(word.substring(i), output);
                }
            }
            return output;
        }
    }