有 Java 编程相关的问题?

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

java填充哈希集在第一次调用时不起作用,只能在第二次调用时起作用

我想做的是:

我想用程序不知道的新词填充哈希集。 用户按下主机上的“转换”按钮。 大型机上给出了包含单词的文件的路径

如果单词是新的,则会打开一个JDialog并要求插入新单词(以便您可以更改拼写,例如第一个字母大…)

如果用户按下JDialog上的“write”按钮,则该单词将添加到哈希集中

但是如果我在那之后打印HashSet,则只显示“旧”值。 当我第二次按下大型机上的“convert”按钮时,所有值都将正确显示在HashSet中

如果有人能帮助我,我将非常感激。 如果需要更多信息,请告诉我。 以下是按下按钮“转换”时ActionListener的代码:

        if (e.getActionCommand().equals(convert.getActionCommand())) {
        try {
            //a file with words is given
            fileHandler = new FileIO(path.getText().trim());

            //lines is a ArrayList<String> and returns all the lines
            lines = fileHandler.readFile();

            for (int i = 0; i < lines.size(); i++) {
                //words is a String[]
                words = lines.get(i).split(" ");
                for (int j = 0; j < words.length; j++) {

                    //hs is a HashSet<String>
                    if (hs.contains(words[j])) {
                        System.out.println("hit: " + words[j]);
                    }else if (!hs.contains(words[j])) {
                        dialog = new JDialog(mainFrame);
                        dialog.setTitle("new Word");
                        dialog.setLayout(new BorderLayout());

                        newWord = new JTextField(words[j].toLowerCase());
                        newWord.selectAll();

                        write = new JButton("write");

                        write.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                //can not use the counters "i" or "j" here otherwise it would be so easy...
                                s = newWord.getText().trim();
                                dialog.setVisible(false);
                                if(dialog != null)
                                    dialog.dispose();
                                if (dialog.isActive()) {
                                    System.out.println("active");
                                }else {
                                    //dead code ??? -- never executed so far
                                    System.out.println("finally....done");
                                }
                            }
                        });

                        dialog.add(newWord, BorderLayout.NORTH);
                        dialog.add(write, BorderLayout.SOUTH);
                        dialog.pack();
                        dialog.setVisible(true);

                        //todo is filled correctly IF pressed "convert Button" the second time
                        if (!s.contentEquals("")) {
                            words[j] = s;
                            hs.add(s);
                            s = "";
                        }

                    }
                } // words


                //Displays the input line but why not the manipulated from the JDialog input?
                StringBuffer sb = new StringBuffer();
                for (String string : words) {
                    sb.append(string);
                    sb.append(" ");
                }
                System.out.println(sb.toString());
                lines.set(i, sb.toString());
                sb.delete(0, sb.length());

            } // lines

当我按下大型机上的“退出”按钮时写入(在文件中)并显示哈希集的代码:

    hashSetHandler = new HashSetIO(WORDS_FILE);
    hashSetHandler.write(hs);
    for (String string : hs) {
        System.out.println(string);

共 (1) 个答案

  1. # 1 楼答案

    你应该考虑做你的^ {< CD1>}模态。

    当前,当您按convert时,会看到许多弹出窗口(从文件中读取的每个新词对应一个),不是吗

    模态和“正常”之间的差异将影响此处的代码:

    dialog.setVisible(true);
    //todo is filled correctly IF pressed "convert Button" the second time
    if (!s.contentEquals("")) {
      words[j] = s;
      hs.add(s);
      s = "";
    }
    

    模式dialog将在dialog.setVisible(true)行阻塞,直到按下write按钮,您的ActionListener将处理事件,设置s = newWord.getText().trim();并处理dialog

    仅在处理此事件(write后,dialog.setVisible(true)后面的代码行将被执行,s将包含newWord.getText().trim()的值

    使用“正常”(非模态)dialog时,您不会在dialog.setVisible(true)处阻塞,因此

    if (!s.contentEquals(""))
    

    行将检查尚未设置的s的某些值(您尚未按下write按钮),测试将失败,if块中的代码将不会执行

    我建议您调试在if行和ActionListener按钮上设置断点的代码。然后,您将更好地理解代码何时执行以及字段的值是什么