我有这个GUI来计算字符串中第一个字母的出现次数。我希望它以列格式计算所有字母,如:
这是我到目前为止所做的:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class Index2 extends JFrame implements ActionListener
{
private JTabbedPane jtabbedPane;
private JPanel characterFinder;
JTextField enterText, countText;
public Index2()
{
setSize(400, 250);
setVisible(true);
setSize(400, 250);
setVisible(true);
setTitle("Total Characters");
setSize(300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
createCharacterFinder();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("Count Characters", characterFinder);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
public void createCharacterFinder()
{
characterFinder = new JPanel();
characterFinder.setLayout(null);
JLabel enterLabel = new JLabel(
"Enter Some Text");
enterLabel.setBounds(90, 5, 260, 20);
characterFinder.add(enterLabel);
enterText = new JTextField();
enterText.setBounds(10, 30, 270, 70);
characterFinder.add(enterText);
JButton search = new JButton("Count Occurences of Each Letter");
search.setBounds(15, 100, 260, 20);
search.addActionListener(this);
characterFinder.add(search);
countText = new JTextField();
countText.setBounds(80, 130, 120, 500);
characterFinder.add(countText);
}
public void actionPerformed(ActionEvent e){
String st=enterText.getText();
char searchedChar=enterText.getText().charAt(0);
count(searchedChar,st);
}
public int count(char c, String str) {
if (str == null) return 0;
int cnt = 0;
for (int i = 0;; cnt++) {
if ((i = str.indexOf(c,i)+1) == 0) break;
}
countText.setText("Character "+c+" occurs "+cnt+" times");
return cnt;
}
public static void main(String[] args)
{
JFrame frame = new Index2();
frame.setSize(300, 700);
frame.setVisible(true);
}
}
答案 0 :(得分:3)
计算字符(假设ASCII字符)的好方法是利用'a'
可以直接映射到数字的事实。
int[] charCounts(String s) {
int[] counts = new int[256]; // maximum value of an ASCII character
char[] c = s.toCharArray();
for (int i=0;i<c.length;++i) {
counts[c[i]]++;
}
return counts;
}
现在查找您可以执行的任何特定元素的计数counts['a']
。
这可能会使代码更加整洁,只需使用上面的方法进行计数,然后只需显示计数:
int[] counts = charCounts("my string");
StringBuilder sb = new StringBuilder();
for (char a = 'a'; a <= 'z'; a++) {
sb.append(a).append(" occurred ").append(counts[a]).append(" times\n");
}
答案 1 :(得分:2)
HashMap<Char, Integer> countedChars = new HashMap<Char, Integer>();
并使用此方法解析字符串的每个字符:
private void parseChar(String c) {
if (!this.countedChars.containsKey(c)) {
this.countedChars.put(c, 1);
} else
this.countedChars.put(c, this.countedChars.get(c)+1);
}
答案 2 :(得分:2)
要计算字符串中的字符出现次数(每个字符的使用频率),我建议使用此优化版本:
在大多数情况下使用int [],对于Unicode字符使用SortedMap<>,避免因自动装箱而导致隐藏的Integer实例化:
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
public class CharCount {
static class MutableInteger {
int value;
public MutableInteger( int v ) {
value = v;
}
public void increment() {
++value;
}
}
public static void main( String[] args ) {
String string = "alabama&é€";
int len = string.length();
int[] freqArray = new int[255];
SortedMap< Character, MutableInteger > freqMap = null;
for( int i = 0; i < len; ++i )
{
char c = string.charAt( i );
if( c < freqArray.length ) {
++freqArray[c];
}
else { // Unicode
if( freqMap == null ) { // first use
freqMap = new TreeMap<>();
}
MutableInteger f = freqMap.get( c );
if( f == null ) {
freqMap.put( c, new MutableInteger( 1 ));
}
else {
f.increment();
}
}
}
for( int i = 0; i < freqArray.length; ++i ) {
int v = freqArray[i];
if( v > 0 ) {
System.out.printf( "%c (%4d) : %d\n", (char)i, i, v );
}
}
if( freqMap != null ) {
for( Entry< Character, MutableInteger > entry : freqMap.entrySet()) {
char c = entry.getKey().charValue();
int v = entry.getValue().value;
System.out.printf( "%c (%4d) : %d\n", c, (int)c, v );
}
}
}
}
输出:
& ( 38) : 1
a ( 97) : 4
b ( 98) : 1
l ( 108) : 1
m ( 109) : 1
é ( 233) : 1
€ (8364) : 1
答案 3 :(得分:1)
计算字符串中第一个字母的出现次数。我希望它能算上所有的字母
这里传递第一个字符
char searchedChar=enterText.getText().charAt(0);
如果你需要考虑每个角色那么
char[] charsToSearch =enterText.getText().toCharArray();//and invoke method for each char
答案 4 :(得分:1)
import java.util.*;
public class CharsCounter {
public Map<Character, Integer> count(String input) {
char[] chars = (input + '\uFFFF').toCharArray();
Arrays.sort(chars);
Map<Character, Integer> charsCount = new LinkedHashMap<Character, Integer>();
int first = 0;
for (int i = 1; i < chars.length; i++) {
if (chars[i] != chars[first]) {
charsCount.put(chars[first], i - first);
first = i;
}
}
return charsCount;
}
}