【发布时间】:2014-12-16 05:32:54
【问题描述】:
好的!我有这个程序用于计算字符串中的单个字符。不管是大写还是小写。如果假设您输入一个字符串,然后询问字符'a',则该程序可以工作,如果只有一个“A”,则计数将读取为零。如果我在 char 搜索中键入“A”,它将计为 1。我的问题是我需要 char 搜索来计算一个字符的所有实例,无论它是大写还是小写,并将它们一起计为一个字符数。
代码如下:
package index;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Index extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected JFrame mainFrame;
protected JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
protected JPanel panel;
private JTextArea textSearch;
private JTextField charSearch;
private JLabel message1;
private JLabel message2;
private char aChar;
public Index()
{
mainFrame = new JFrame("Character Finder");
mainFrame.setBounds(100, 100, 450, 300);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane.setBounds(0, 0, 434, 261);
mainFrame.getContentPane().add(tabbedPane);
panel = new JPanel();
tabbedPane.addTab("Index", panel);
panel.setLayout(null);
message1 = new JLabel("Enter text to be search: ");
message1.setBounds(79, 55, 140, 27);
panel.add(message1);
textSearch = new JTextArea();
textSearch.setBounds(220, 25, 223, 94);
panel.add(textSearch);
message2 = new JLabel("Enter a charater: ");
message2.setBounds(179, 159, 100, 14);
panel.add(message2);
charSearch = new JTextField();
charSearch.setBounds(285, 156, 64, 20);
panel.add(charSearch);
mainFrame.setSize(500, 350);
mainFrame.setVisible(true);
textSearch.setVisible(true);
textSearch.setEditable(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
TextSearchHandler tHandler = new TextSearchHandler();
charSearch.addActionListener(tHandler);
}
class TextSearchHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int count = 0;
int current = 0;
String instring;
String inChar;
instring = textSearch.getText();
if(instring.equals(""))
{
instring = ("");
textSearch.setText("");
}
inChar = charSearch.getText();
if(inChar.equals(""))
{
inChar = ("");
charSearch.setText("");
}
current = instring.indexOf(inChar, 0);
while(current != -1)
{
inChar.equalsIgnoreCase(instring);
aChar = instring.charAt(0);
current = instring.indexOf(inChar, current + 1);
count++;
}
JOptionPane.showMessageDialog(null, "Character " + "'" + inChar + "'" + " count: "
+ count++, "Results", JOptionPane.PLAIN_MESSAGE);
}
}
public static void main(String[] args)
{
new Index();
}
}
当我进行字符搜索时,我必须输入两次。我必须输入大写版本来计算那个版本,删除那个,然后输入小写版本来计算那个。任何帮助,将不胜感激。
【问题讨论】: