我在Applet中使用一个按钮向LinkedList添加单词。但它目前无法正常工作。似乎每次选择“添加单词”按钮时,它将前一个数字保存为计数值,并且使用旧值递增,而不是每次都以1为单位递增。当选择搜索按钮并输出错误的数字时,这会导致打印错误的值。尽管选择了“添加单词”的次数,但如何使计数变量保持正确的值。
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");
}
}
}
}
}
答案 0 :(得分: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()
查找有效单词的数量。你不必为此使用单独的计数器。