有 Java 编程相关的问题?

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

java如何停止小程序中计数变量的错误递增?

我正在使用小程序中的按钮向LinkedList添加单词。但是,它目前无法正常工作。似乎每次选择“添加单词”按钮时,它都会保留上一个数字作为计数值,并随着旧值递增,而不是每次都以1递增。这会导致在选择搜索按钮并输出错误号码时打印不正确的值。如何使count变量保持正确的值,而不管选择“Add word”的次数如何

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;
    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 AddHandler(this));
        but1.addActionListener(new ClearHandler(this));
        but5.addActionListener(new SearchHandler(this));
        but2.addActionListener(new RemoveFirstHandler(this));


    }

    class AddHandler implements ActionListener {

        private Applet2 theApplet;

        public AddHandler(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");
                    list.add(text);

这就是变量

                    count = count + 1;

                } else {
                    jLabel.setText("ERROR - Please enter a word");
                }

            }

        }
    }

    class ClearHandler implements ActionListener {

        private Applet2 theApplet;

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

        public void actionPerformed(ActionEvent e) {

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

        }
    }

    class SearchHandler implements ActionListener {

        private Applet2 theApplet;

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

        public void actionPerformed(ActionEvent e) {

            String text = theApplet.value1.getText();

            ListIterator<String> listIterator = list.listIterator();

            while (listIterator.hasNext()) {


                if (text.equals(listIterator.next())) {

                    search_count = search_count + 1;

                }
            }

            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);

            }

        }
    }

    class RemoveFirstHandler implements ActionListener {

        private Applet2 theApplet;

        public RemoveFirstHandler(Applet2 app) {
            theApplet = app;
        }
        public void actionPerformed(ActionEvent e) {

            ListIterator<String> listIterator = list.listIterator();

            while (listIterator.hasNext()) {

                if (text.equals(listIterator.next())) {

                    list.remove(text);
                    jLabel.setText("First occurrence of word " + "'" + text + "'" + " has been deleted");
                    count = 0;
                    search_count = 0;
                    break;

                } else {

                    jLabel.setText("ERROR - Word " + "'" + text + "'" + " is not in list");
                }


            }

        }

    }
}

共 (1) 个答案

  1. # 1 楼答案

    好的,首先,不要向任何人显示此代码。他们不会留下深刻印象。有许多方法可以确定输入的单词是否为整数,但使用NumberFormatException不是其中之一

    相反,您应该使用以下内容:

    if(!text.trim().matches("[\\d]+")) {
       //the text entered is not an Integer. Do your magic here.
    }
    

    如果希望输入的文本不包含任何数字,请使用

    Pattern and Matchers
    Pattern pattern = Pattern.comile("[\\d]");
    Matcher matcher = pattern.matcher(text);
    if(!matcher.find()) {
       //matcher was unable to find any number in the string
    }
    

    在内部,您正在维护一个有效单词列表,您称之为list。您可以使用list.size()查找您拥有的有效单词数。你不必使用单独的计数器