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

标签 java recursion io

我正在尝试编写一个程序,该程序将从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;
    }  
} 

最佳答案

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

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 ;

}  

}

关于java - java递归将一个单词分成三个或更多单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55840103/

相关文章:

Java <Optional> 对象为空或对象的属性为空?

递归调用自身的mysql存储过程

arrays - 递归无单位元素类型

recursion - 如何评估递归宏定义

java - 监控 InputStream 的最佳方式是什么?

c# - 进程无法访问该文件,因为它正被另一个进程使用

scala - Scala 分配评估 Unit 而不是分配的值的动机是什么?

java - 从 linux 上的 groovy 脚本运行 java 可执行文件

java - 检查JRE是否安装在不同的操作系统上?

java - 如何实现 ExecutorService 来轮流执行任务?