有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:乱码程序

我的程序加载两个文本文件:一个是英文单词列表,另一个是乱码(更多的是随机字符串,但大多数是生成单词),然后确定哪些单词可以由乱码组成,并打印(至少应该是这样的)该单词及其旁边的乱码版本。我的程序有效地找到了这些乱七八糟的单词可以组成的单词。问题不是所有的单词都有对应的混淆,而是在旁边打印混淆的单词。我还需要右边的乱七八糟的词,右边的解读。这里的示例是一些输出(逗号分隔行,即如果两个单词是逗号,则它们相邻打印; addej, 一种等级制度, 阿尔万海军, 安娜阿布香蕉, 巴尔托克, 布兰尼在附近

   public class Lab4{

    public static void main(String [] args) throws Exception{
        if(args.length<2) {
            Error(); }
        BufferedReader jumbledW = new BufferedReader(new FileReader(args[1]));
        BufferedReader words = new BufferedReader(new FileReader(args[0]));
        List<String> jumbledWList = new ArrayList<>();
        List<String> wList = new ArrayList<>();

        long initialTime = System.currentTimeMillis();

        while(jumbledW.ready()){
            String jumble = jumbledW.readLine();
            jumbledWList.add(jumble);
        }

        Collections.sort(jumbledWList);
        while (words.ready()){
            String word = words.readLine();
            wList.add(word);
        }
        Collections.sort(wList);
        for (String jumble : jumbledWList ) {
            System.out.print(jumble + " ");
            for (String word : wList) {
                if(toConnical(jumble).equals(toConnical(word)))
                    System.out.print(word);


        }
        System.out.println();   
        }
        long finalTime = System.currentTimeMillis();
        long time = finalTime - initialTime;
        System.out.println("The time taken for this program to run is " + time/1000.0 + " seconds" );

        }

    private static void Error(){
        System.out.println("\nError:You have to pass the name of the input files on the command line" );
        System.exit(0);
        }   

    private static String toConnical(String word){
        char [] arr = word.toCharArray();
        Arrays.sort(arr);
        String connical = new String(arr);
        return connical;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    当我跳过那些没有回答的旧帖子时,我遇到了这个特别的问题,坦率地说,这个问题有点不清楚实际的问题可能是什么。我读这篇文章的方式是:

    1. 文件名通过命令行传递给这个应用程序,命令行由几个混杂的字符组成。但是,不确定文件中的每一行是由单个混杂的单词组成还是文件中的每一行都由多个混杂的单词组成,这些单词之间用一个或多个空格隔开(甚至可能是制表符)。考虑到这一点,我们需要涵盖任何一种情况

    2. 另一个文件名也通过命令行传递给该应用程序,该命令行由几个有效的语言类型单词组成。此文件被视为单词列表,而任何单个混乱单词都可能属于列表中的一个(或多个)单词,如果未对其进行解读(混乱字符串列表中的单词可能未被混淆)

    3. 处理混杂字符串列表时,每个单词列表能够通过排序后的字符对字符的比较与排序后的混杂单词字符进行匹配的单词将打印到控制台窗口

    4. 控制台输出必须是逗号分隔的字符串,由每个混乱的单词组成,后跟单词列表中以空格分隔的匹配单词:

      addej jaded、AHICHRY层级、阿尔万·阿尔万海军、安娜阿布·香蕉,。。。等

      然而,这似乎与您的评论相矛盾:

      "the program is given a txt file of a great deal of english words youd find in a dictionary and another txt file with jumbled words such as cra which could make car or rat. The output i desire is the in reference to the example of cra would be "car cra" (on one line)."

      然而,空格分隔的单词列表单词排在第一位,然后处理混杂的单词,每个单词使用一个控制台窗口行。需要哪种格式?顺便说一句,大鼠不能通过cra实现

    实际上,您的代码按预期工作,但由于您使用的是BufferedReader对象和FileReader对象,因此您的代码需要位于try/catch块中,以处理任何异常,例如FileNotFoundExceptionIOException。这是一项要求,不能排除

    下面是为适应您的第一个所需输出格式而稍微修改的代码:

    try {
        BufferedReader jumbledW = new BufferedReader(new FileReader(args[1]));
        BufferedReader words = new BufferedReader(new FileReader(args[0]));
        List<String> jumbledWList = new ArrayList<>();
        List<String> wList = new ArrayList<>();
    
        long initialTime = System.currentTimeMillis();
    
        while (jumbledW.ready()) {
            String jumble = jumbledW.readLine();
            jumbledWList.add(jumble);
        }
    
        Collections.sort(jumbledWList);
        while (words.ready()) {
            String word = words.readLine();
            wList.add(word);
        }
        Collections.sort(wList);
    
        String resultString = "";
        for (int i = 0; i < jumbledWList.size(); i++){
            String jumble = jumbledWList.get(i);
            resultString+= jumble + ":";
            for (String word : wList) {
                if (toConnical(jumble).equals(toConnical(word))) {
                    resultString+= " " + word;
                }
            }
            if (i != (jumbledWList.size()-1)) { resultString+= ", "; }
        }
        System.out.println(resultString);
    
        long finalTime = System.currentTimeMillis();
        long time = finalTime - initialTime;
        System.out.println("The time taken for this program to run is " + time / 1000.0 + " seconds");
    } 
    catch (FileNotFoundException ex) { ex.printStackTrace(); } 
    catch (IOException ex) { ex.printStackTrace(); }
    

    混杂词列表文件包含以下混杂字符串:

    addej
    ahicryrhe
    alvan
    annaab
    baltoc
    braney
    cra
    htis
    

    在我的370101单词列表文件中运行了上面的混乱单词列表后,控制台输出是这样的。在我的系统上处理花费了0.740秒:

    addej: jaded, ahicryrhe: hierarchy, alvan: alvan naval, annaab: banana, baltoc: cobalt, braney: barney nearby, cra: arc car, htis: hist hits isth shit sith this tshi
    

    上面显示的所有单词都在我的单词列表文件中