通过字典数组迭代

时间:2018-01-29 20:56:01

标签: java arrays string loops nested-loops

我有一个包含一首故意拼写错误的诗歌的数组。我试图通过String数组迭代来识别拼写错误,方法是将String数组与包含字典的String数组进行比较。如果可能的话,我想要一个允许我继续使用嵌套for循环的建议

 for (int i = 0; i < poem2.length; i++) {
        boolean found = false;
        for (int j = 0; j < dictionary3.length; j++) {
            if (poem2[i].equals(dictionary3[j])) {
                found = true;
                break;
            }
        }
        if (found==false) {
            System.out.println(poem2[i]);
        }
    }

输出正在打印出正确拼写的单词以及拼写不正确的单词,我的目标是只打印拼写错误的单词。以下是我填写&#39;字典3&#39;和#poem2&#39;阵列:

      char[] buffer = null;
      try {
        BufferedReader br1 = new BufferedReader(new 
    java.io.FileReader(poem));
        int bufferLength = (int) (new File(poem).length());
        buffer = new char[bufferLength];
        br1.read(buffer, 0, bufferLength);
        br1.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }

    String text = new String(buffer);
    String[] poem2 = text.split("\\s+");

    char[] buffer2 = null;
    try {
        BufferedReader br2 = new BufferedReader(new java.io.FileReader(dictionary));
        int bufferLength = (int) (new File(dictionary).length());
        buffer2 = new char[bufferLength];
        br2.read(buffer2, 0, bufferLength);
        br2.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }

    String dictionary2 = new String(buffer);
    String[] dictionary3 = dictionary2.split("\n");

3 个答案:

答案 0 :(得分:1)

你的基本问题在线

String dictionary2 = new String(buffer);

您尝试转换代表buffer2中存储的字典的字符,但您使用了buffer(没有2后缀)。这种命名变量的方式可能表明你需要一个循环,或者在这种情况下单独的方法将返回它所拥有的单词的选定文件数组(你也可以添加为应该拆分字符串的方法参数分隔符)。

所以dictionary2来自buffer的{​​{1}}字符代表诗歌,而不是字典数据。

另一个问题是

String[] dictionary3 = dictionary2.split("\n");

因为您只在\n上进行分割,但某些操作系统(如Windows)使用\r\n作为行分隔符序列。因此,您的字典数组可能包含foo\r而不是foo之类的字词,这会导致poem2[i].equals(dictionary3[j]始终失败。

要避免此问题,您可以在\\R(自Java 8以来可用)或\r?\n|\r上拆分。

您的代码中存在其他问题,例如在try部分中关闭资源。如果之前将抛出任何异常,则永远不会调用close()而留下未关闭的资源。要解决这个问题,请关闭finally部分中的资源(在尝试后始终执行 - 无论是否抛出异常),或者更好地使用try-with-resources

顺便说一句,您可以简化/澄清负责从文件中读取单词的代码

List<String> poem2 = new ArrayList<>();
Scanner scanner = new Scanner(new File(yourFileLocation));
while(scanner.hasNext()){//has more words
    poem2.add(scanner.next());
}

对于字典而不是List,您应该使用Set/HashSet来避免重复(通常在检查它们是否包含某些元素时,集合也会有更好的性能)。这样的集合已经提供了像contains(element)这样的方法,所以你不需要那个内循环。

答案 1 :(得分:0)

我复制了你的代码然后运行它,我发现了两个问题。好消息是,两者都是非常快速的修复。

<强>#1 当我在读入后将dictionary3中的所有内容打印出来后,它与poem2中的所有内容完全相同。您在代码中读取字典中的这一行是问题所在:

 String dictionary2 = new String(buffer);

你正在使用buffer,这是你在诗中读到的变量。因此,缓冲包含诗,你的诗和字典最终相同。我想你想用buffer2代替,这是你在字典中读到的内容:

 String dictionary2 = new String(buffer2);

当我改变它时,字典和诗似乎有适当的条目。

<强>#2 另一个问题,正如Pshemo在他们的答案中指出的那样(这是完全正确的,并且是一个非常好的答案!)就是你在\n分裂了字典。我唯一能说出与Pshemo不同的是,你应该像你对这首诗一样分开\\s+,保持一致。事实上,当我调试时,我注意到字典单词都附加了“\ r”,可能是因为你在\n分裂了。要解决此问题,请更改此行:

String[] dictionary3 = dictionary2.split("\n");

对此:

String[] dictionary3 = dictionary2.split("\\s+");

尝试更改这两行,并告诉我们是否可以解决您的问题。祝你好运!

答案 2 :(得分:-1)

将字典转换为ArrayList,改为使用Contains

这样的事情应该有效:

if(dictionary3.contains(poem2[i])
   found = true;
else
   found = false;

使用这种方法你也可以摆脱那个嵌套循环,因为contains方法会为你处理。

您可以使用以下方法将Dictionary转换为ArrayList: new ArrayList<>(Arrays.asList(array))

相关问题