java - 在一组字符串中搜索字符
问题描述
我制作了这个程序,您可以在其中输入一个字符串,它会记录它并向用户显示该字符串中的字符数和输入的字符串数。例如,如果用户在文本框中输入“Sally”并单击按钮,它会说
串数:1
字符数:5
并且用户可以根据需要输入更多字符串,例如“Alex”,这会将数字从 1 字符串更改为 2,将字符数更改为 9
无论如何,我想这样做,所以有第二个文本框,用户可以在其中输入几个字符并搜索这些字符,以查看用户先前输入的字符串中有多少包含用户搜索的字符
下面是它应该做什么的一个例子: 所以如果用户输入:“Sally”、“Jack”和“Dachshunds”,程序应该读回说有 3 个字符串和 20 个字符。
现在,如果用户在搜索框中输入“ac”,程序会说“带有 'ac' 的字符串数为 2(字符串 Jack 和 Dachshunds 包含 'ac')
如果我上面所说的内容令人困惑,这是我的整个程序的代码
我只需要有关如何编写用于在这些字符串中搜索特定字符的部分的帮助
我知道有一个叫做 get.contains() 的东西,但我试图实现它,但我遇到了麻烦
import javax.swing.*;
import java.awt.event.*;
public class good{
StringSet sSet;
JTextField inputStr;
JLabel numStr;
JLabel numChar;
JTextField inputStr2;
JLabel numofStr;
good() {
sSet=new StringSet();
JFrame f= new JFrame("StringSetYetAgain");
JLabel en=new JLabel("Enter a String:");
en.setBounds(50, 10, 100, 100);
numStr=new JLabel("Number of Strings: 0");
numStr.setBounds(50, 160, 400, 100);
numChar=new JLabel("Number of Characters: 0");
numChar.setBounds(50, 180, 400, 100);
inputStr=new JTextField();
inputStr.setBounds(50, 80, 400, 50);
JButton pushMe=new JButton("Push to include String");
pushMe.setBounds(50, 140, 400, 50);
JLabel enter=new JLabel("String to search for:");
enter.setBounds(50, 230, 400, 100);
inputStr2=new JTextField();
inputStr2.setBounds(50, 300, 400, 50);
JButton pushMe2=new JButton("Search");
pushMe2.setBounds(50, 360, 400, 50);
numofStr=new JLabel("Number of strings with: 0");
numofStr.setBounds(50, 405, 400, 50);
pushMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s=inputStr.getText();
sSet.add(s);
String ns=numStr.getText();
ns=ns.substring(0, ns.length() - 1)+sSet.size();
numStr.setText(ns);
String nc=numChar.getText();
nc=nc.substring(0, nc.length() - 1)+sSet.numChars();
numChar.setText(nc);
inputStr.setText("");
}
});
//I would add the code for the search part here
f.add(en);
f.add(inputStr);
f.add(pushMe);
f.add(numStr);
f.add(numChar);
f.add(enter);
f.add(inputStr2);
f.add(pushMe2);
f.add(numofStr);
f.setSize(550,600);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new good();
}
}
这是 StringSet 类的源代码
public class StringSet {
private String[] arr;
private int count;
public StringSet() {
arr = new String[200];
count = 0;
}
void add(String newStr){
arr[count++] = newStr;
}
int size(){
return count;
}
int numChars(){
int total = 0;
for(int i = 0; i < count; ++i){
total += arr[i].length();
}
return total;
}
int countStrings(int len){
int total = 0;
for(int i = 0; i < count; ++i){
if(arr[i].length() == len)
++total;
}
return total;
}
}
这是到目前为止搜索字符的部分
pushMe2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
public int countOf(String substring, StringSet sSet) {
int counter = 0;
for (int i = 0; i < sSet.size(); i++)
if (sSet.get(i).contains(substring))
counter++;
return counter;
}
}
});
解决方案
在你说的地方添加
我将在此处添加搜索部分的代码
buttonThatShouldCount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int counter = 0;
String substring = inputStr2.getText();
for (int i = 0; i < sSet.size(); i++)
if (sSet.get(i).contains(substring))
counter++;
inputStr2.setText("The substring \"" + substring "\" occurs " + count + " times.");
}
}
您还需要将以下方法添加到您的StringSet
类中:
public String get(int index) {
return arr[index];
}
我们需要这样做,因为否则无法访问存储在集合中的字符串。
推荐阅读
- javascript - 在同一存储库中的多个项目/文件夹/应用程序之间共享代码
- java - 是否可以为文本设置外部笔划?
- swift - 当点击表格视图的单元格时引导用户进行注释
- angular - 如果为 TabGroup 设置了 [backgroundColor] 属性,Angular Material 2 按钮会失去波纹
- regex - re.sub 不替换匹配
- sql - 在选择结果中排序。Postgres
- tcpdf - PHP TCPDF - 多个 pdf 支持?
- python - 如何在pymc2中为多类分类制作一个简单的贝叶斯神经网络
- jhipster - Ag-grid 不会在 JHipster 应用程序中呈现(Angular 7 + Webpack 4)
- reactjs - 使用passport-google-oauth20在快速反应应用程序中谷歌登录后重定向问题