如何正确计算我的计数变量?

时间:2015-11-03 19:08:01

标签: java

我正在尝试创建一个使用文本字段将字符串添加到链接列表的Java applet。我无法使用搜索按钮。我试图在文本字段中获取用户指定的字符串,然后搜索列表并打印该单词被查找的次数。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 * Created by joshuaogunnote on 31/10/2015.
 */

public class Applet2 extends JApplet {

    private String text;
    private int text1;
    JTextField value1, value2;
    LinkedList<String> list = new LinkedList<String>();
    public JLabel jLabel;
    public int count = 0;

我创建了这个search_count变量来计算找到该单词的次数。

    public int search_count = 0;

    public void init() {

        JLabel prompt = new JLabel("Please enter a word");
        JLabel prompt1 = new JLabel("Please enter a certain letter");

        value1 = new JTextField(10);
        value2 = new JTextField(10);

        JPanel textPanel = new JPanel();
        textPanel.add(prompt);
        textPanel.add(value1);
        add(textPanel, BorderLayout.NORTH);
        textPanel.add(prompt1);
        textPanel.add(value2);


        JPanel centrePanel = new JPanel();
        text = "";
        jLabel = new JLabel(text);
        centrePanel.add(jLabel);
        add(centrePanel, BorderLayout.CENTER);


        JButton but = new JButton("Add word");
        JButton but1 = new JButton("Clear");
        JButton but2 = new JButton("Remove first occurrence");
        JButton but3 = new JButton("Remove all occurrences");
        JButton but4 = new JButton("Display all words begging with certain letter");
        JButton but5 = new JButton("Search");

        JPanel butPanel = new JPanel();

        butPanel.add(but);
        butPanel.add(but1);
        butPanel.add(but5);
        butPanel.add(but2);
        butPanel.add(but3);
        butPanel.add(but4);


        add(butPanel, BorderLayout.SOUTH);

        but.addActionListener(new ButtonHandler(this));
        but1.addActionListener(new ButtonHandler1(this));
        but5.addActionListener(new ButtonHandler2(this));

    }

    class ButtonHandler implements ActionListener {

        private Applet2 theApplet;

        public ButtonHandler(Applet2 app) {
            theApplet = app;
        }

        public void actionPerformed(ActionEvent e) {

            text = theApplet.value1.getText();

            try {

                text1 = Integer.parseInt(text);
                jLabel.setText("ERROR - The string " + "'" + text1 + "'" + " is not a valid word");

            } catch (NumberFormatException e1) {

                if (text.length() != 0) {
                    jLabel.setText("Word " + "'" + text + "'" + " has been added to the list");
                    count = count + 1;
                } else {
                    jLabel.setText("ERROR - Please enter a word");
                }

            }

        }
    }

    class ButtonHandler1 implements ActionListener {

        private Applet2 theApplet;

        public ButtonHandler1(Applet2 app) {
            theApplet = app;
        }

        public void actionPerformed(ActionEvent e) {

            list.clear();
            jLabel.setText("List has been cleared");
            count = 0;

        }
    }

    class ButtonHandler2 implements ActionListener {

        private Applet2 theApplet;

        public ButtonHandler2(Applet2 app) {
            theApplet = app;
        }

        public void actionPerformed(ActionEvent e) {

            String text = theApplet.value1.getText();

这里我试图使用for循环迭代列表中的所有字符串,如果找到匹配则增加search_count。然而,它并没有产生正确的答案。当用户尝试搜索不在列表中的单词时,我也尝试生成ERROR消息。如何获取search_count变量以及如何在正确的时间显示ERROR消息?

            for (int i = 0; i < list.size(); i++) {

                if(text.equals(list.get(i))){

                    search_count = search_count + 1;
                } else {
jLabel.setText("ERROR - word is not in the list")


            }

            jLabel.setText("Word " + "'" + text + "'" + " was found " + search_count + " time(s) in the list");

            if (text.length() == 0) {

                jLabel.setText("Please enter a word - The total number of words in the list are: " + count);

            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

在循环中使用它之前,似乎没有将count声明为变量。如果输出不是您想要/期望的,那么循环的条件会为您提供其他内容,而不是您的想法。

int count = 0;