java如何統計字元串中每個字元出現的次數

正確答案:

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Test {

@SuppressWarnings("unchecked")
public static void main(String[] args) {

String str = null;
try {
str = args[0];
} catch ( e) {
System.out.println("請輸入參數!");
System.exit(0);
}
Map tree = new TreeMap();

for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if (!tree.containsKey(ch)) {
tree.put(ch, new Integer(1));
} else {
Integer in = (Integer) tree.get(ch) + 1;
tree.put(ch, in);
}
}
}
Iterator tit = tree.keySet().iterator();
while (tit.hasNext()) {
Object temp = tit.next();
System.out.print(temp.toString() + "(" + tree.get(temp) + ")");
}
}
}

㈡ 怎樣統計一串字元串字元出現的次數java

直接上代碼
import java.util.*;
public class Test {
public static void main(String args[]){
String s = "abcdad"; //待測試的字元串
Map<Character, Integer> result = getCharMaps(s);
System.out.println(result);//列印出字元串中各字內符出現的次容數!

}
public static Map<Character, Integer> getCharMaps(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
Integer count = map.get(c);
map.put(c, count == null ? 1 : count + 1);
}
return map;

}

}

㈢ java判斷字元串在字元串中出現的次數

int sum=0;
int count=0;
do{
int count=str.indexOf("ok");
if(count>=0)
{
sum++;

str=str.subString(count+"ok".length,str.length)

}
}wille(count=-1)
sysout(sum)

㈣ java中怎麼實現求字元串中某個字母出現的次數

str為你要測試的字元串第一種方法:byte[]temp=str.getBytes();//使用平台默認的字元集將此String解碼為位元組序列,並將結果存儲到一個新的位元組數組中。intcount=0;//遍歷數組的每一個元素,也就是字元串中的每一個字母for(inti=0;i<temp.length;i++){//如果字母等於cif(temp[i].equals('c')){//計數器加一count++;}}第二種:intcount=0;Stringstr=//你要測試的字元串//index為字元串中第一次出現c的位置,如果字元串中沒有c將返回-1intindex=str.indexOf(c);//如果字元串中有cwhile(str.indexOf(c)!=-1){count++;//將字元串出現c的位置之前的全部截取掉str=str.subString(str.indexOf(c));}考慮大小寫:str=str.toLowerCase();//將字元串全部轉化成小寫

㈤ Java中,在一串字元串中,指定字元串出現的次數

前面錯誤的寫法 : C你定義了是一個變數 ,並且已經賦值了。後續的代碼也沒有對它做任何處理。值就不會根據「java」出現的位置改變了

後面一種寫法,C在循環裡面 ,他是會根據「java」的位置不斷改變。

㈥ java 怎樣從一個string字元串中判斷某個字母出現的次數

import java.util.Scanner;
public class test{
public static void main(String[] args) {
/*
判斷是否是字母,將輸入的字元串一個個取出,然後轉換成char型,
然後再將char型強制轉換成int型,這時候返回的是一個ASCII碼,
ASCII碼在97到122之間是小寫字母,ASCII碼在64到90之間是大寫字母。
* */
int count = 0;
System.out.print("請輸入一個字元串:");
String str = new Scanner(System.in).next();
//輸入字元串後判斷是不是含英文字母的字元串
for (int i = 0; i < str.length(); i++) {
if (((int) str.substring(i, i + 1).charAt(0) >= 97 && (int) str
.substring(i, i + 1).charAt(0) <= 122)
|| ((int) str.substring(i, i + 1).charAt(0) >= 64 && (int) str
.substring(i, i + 1).charAt(0) <= 90)) {
count++;
}
}
while (1 == 1) {
//當count大於等於1的時候說明這個字元串中,含有字母
if (count>=1) {
break;
}else{
//如果不是含字母的字元串,就繼續循環,直到輸入含字母的字元串
System.out.println("你輸入的不是含字母的字元串!");
System.out.println(" ");
System.out.print("請輸入一個字元串:");
str = new Scanner(System.in).next();
for (int i = 0; i < str.length(); i++) {
if (((int) str.substring(i, i + 1).charAt(0) >= 97 && (int) str
.substring(i, i + 1).charAt(0) <= 122)
|| ((int) str.substring(i, i + 1).charAt(0) >= 64 && (int) str
.substring(i, i + 1).charAt(0) <= 90)) {
count++;
}
}
}
}
//後面還要使用count統計,所以此處要將count清零
count = 0;
System.out.print("請輸入你要查找的字母:");
String scanf = new Scanner(System.in).next();
//輸入要查找的字元串後判斷是不是只有一個字母,或者是不是一個字母
while (1 == 1) {
if ((((int) scanf.charAt(0) >= 97 && (int) scanf.charAt(0) <= 122) || ((int) scanf
.charAt(0) >= 64 && scanf.charAt(0) <= 90))
&& (scanf.length() == 1)) {
break;
} else {
System.out.println("你輸入的不是一個英文字母!");
System.out.println(" ");
System.out.print("請輸入一個你要查找的字母:");
scanf = new Scanner(System.in).next();
}
}
//統計字母在那個字元串中出現的次數
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals(scanf)) {
count++;
}
}
if (count == 0) {
System.out.println("字母" + scanf + "不存在字元串:" + str + "中!");
} else {
System.out.println("字母" + scanf + "在字元串:" + str + "中出現了" + count
+ "次!");
}
}
}