确定!我有这个程序用于计算字符串中单个字符的类。无论是大写还是小写。如果你输入一个字符串,该程序有效,然后询问char 'a'
,如果只有一个' A'计数将读为零。如果我输入' A'在char搜索中它将计数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();
}
}
当我进行char搜索时,我必须输入两次。我必须键入大写版本来计算该版本,擦除它,然后小写版本来计算。任何帮助,将不胜感激。
答案 0 :(得分:1)
您可以在输入和要搜索的字母上使用String. toUpperCase()
看看tutorialspoint
答案 1 :(得分:1)
比较
时,您可以使用字符串的equalsIgnoreCase()
方法
答案 2 :(得分:1)
我稍微修改了您的代码,以接受有关搜索忽略大小写的用户输入。如果检查忽略大小写,则使用搜索字符串的小写版本和要搜索的文本来完成搜索。您可以修改它并根据实际需要进行增强
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
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 JCheckBox ignoreCaseCheck;
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);
ignoreCaseCheck = new JCheckBox("Ignore case while search.");
ignoreCaseCheck.setBounds(179, 176, 200, 20);
panel.add(ignoreCaseCheck);
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 = ignoreCaseCheck.isSelected() == true ? textSearch.getText().toLowerCase() : textSearch.getText();
if (instring.equals(""))
{
instring = ("");
textSearch.setText("");
}
inChar = ignoreCaseCheck.isSelected() == true ? charSearch.getText().toLowerCase() : charSearch.getText();
if (inChar.equals(""))
{
inChar = ("");
charSearch.setText("");
}
current = instring.indexOf(inChar, 0);
while (current != -1)
{
inChar.equalsIgnoreCase(instring);
instring.charAt(0);
current = instring.indexOf(inChar, current + 1);
count++;
}
JOptionPane.showMessageDialog(null, "Character " + "'" + charSearch.getText() + "'" + " count: " + count++, "Results", JOptionPane.PLAIN_MESSAGE);
}
}
public static void main(String[] args)
{
new Index();
}
}`
答案 3 :(得分:0)
谢谢你们,我们从你们每个人那里获得了一些见解,并做了以下改动。
instring = textSearch.getText().toLowerCase();
if(instring.equals(""))
{
instring = ("");
textSearch.setText("");
}
inChar = charSearch.getText().toLowerCase();
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 " + "'" + charSearch.getText() + "'" + " count: "
+ count++, "Results", JOptionPane.PLAIN_MESSAGE);
现在它完美无缺!我很感激!