導航:首頁 > 編程語言 > java字元串數字個數

java字元串數字個數

發布時間:2022-08-24 11:34:33

⑴ 如何用java String 查找字元個數

import java.util.Scanner;
public class test3 {
public static void main(String [] args){
System.out.print("inuput : ");
Scanner input=new Scanner(System.in);
int [] digit =new int[256];
String inputString = input.next();
//為了避免在輸入的時候錯誤的鍵入了空格和table,進行一下處理
//以空格為分隔符分割字元串
String [] strings=inputString.split(" ");
String inputString1="";
//將分割後的字元串重新連接起來
for(String str : strings){
inputString1+=str;
}
//以table為分隔符分割字元串
strings=inputString.split(" ");
//將分割後的字元串重新連接起來
String inputString2="";
for(String str : strings){
inputString2+=str;
}
String returnString="";
for (int i = 0; i < inputString2.length(); i++) {
digit[inputString.charAt(i)-'!']++;
//將重復的字元輸出,這里為什麼是2呢有人會問重復次數也可以大於2啊?
//是因為大於2的在2的時候已經被輸出,為了避免重復次數大於2的字元被重復輸出,所以這里只能是2
if(digit[inputString.charAt(i)-'!']==2){
returnString+=inputString.charAt(i);
}
}
System.out.print(returnString);
}
}


樓主算是問對人了

馬上給答案


樓主雖然我這個最終目的不是統計每個字元的個數,但是代碼裡面已經實現了這個功能,只要稍作修改就可以,我的主要功能是輸出重復的字元!


希望我給的代碼樓主還滿意

⑵ 怎樣用java寫這個程序:定義一個函數,用於統計輸入字元串中數字字元的個數

public class StringUtils001 {

public static void main(String[] args) {
String input = getInputString();
int numCount = countNum(input);

JOptionPane.showMessageDialog(null, "字元串 [" + input + "] 中數字的個數為:" + numCount );
}

/** 計算字元串中數字的個數 */
public static int countNum(String str){
int count = 0;
for(char c:str.toCharArray()){
if(Character.isDigit(c)){
count++;
}
}
return count;
}

private static String getInputString() {
String input = null;
while(true){
input = JOptionPane.showInputDialog("請輸入字元串");
if(input == null || input.trim().length() == 0)
JOptionPane.showMessageDialog(null, "忽悠我是吧, 別正個空的字元串啊 ~");
else
return input;
}
}
}

⑶ 編寫java程序統計字元個數。

inteng=0;
intspace=0;
intnum=0;
intother=0;
Scannersc=newScanner(System.in);
System.out.println("請輸入");
Stringstr=sc.next();
char[]arr=str.charAt();
for(inti=0;i<arr.length;i++){
if((arr[i]>='A'&&arr[i]<='Z')||(arr[i]>='a'&&arr[i]<='z')){
eng++;
}elseif(arr[i]==''){
space++;
}elseif(arr[i]>='0'&&arr[i]<='9'){
num++;
}else{
other++;
}
}
System.out.println("英語字母個數:"+eng);
System.out.println("空格個數:"+space);
System.out.println("數字個數:"+num);
System.out.println("其他個數:"+other);

⑷ 使用java語言編寫程序,統計從控制台輸入的一行字元串中數字個數、字母個數和其他字元個數,並列印輸出。

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String intpatten="[0-9]";
String charpatten="[a-zA-Z]";
String signalpatten="\\W";
int intflag=0;
int charflag=0;
int signalflag=0;
String str=sc.next();
String[] strs = new String[str.length()];
for(int i=0;i<str.length();i++) {
strs[i]=str.substring(i,i+1);
if(strs[i].matches(intpatten))
intflag++;
else if(strs[i].matches(charpatten))
charflag++;
else
signalflag++;
}
System.out.println("字元個數:"+charflag+" 數字個數"+intflag+" 非字元數:"+signalflag);

}
正則表達式做 看不懂追問

⑸ java怎麼統計一個字元的個數字

int i;
06
/***countDigit統計數字的數目
07
* countLetter統計字母的數目
08
* countLetter統計其餘的數目
09
*/
10
int countDigit=0, countLetter=0, countOthers=0;
11

12
String input = 「dfdfdf23dfdjk8989」;
13

14
//將字元串變數轉化為字元數組
15
char[] charArray = input.toCharArray();
16
for(i=0;i<charArray.length;i++)
17
{
18
//ASIIC碼
19
if(charArray[i]<='z'&&charArray[i]>='a' || charArray[i]<='Z'&&charArray[i]>='A')
20
countLetter++;
21
else if(charArray[i]<='9' && charArray[i]>='0' )
22
countDigit++;
23
else
24
countOthers++;
25
}
26
System.out.println("the number of letter "+countLetter);
27
System.out.println("the number of digit "+countDigit);

⑹ java編程:輸入一個字元串,計算字元串中所包含的字母個數,數字個數,漢字個數!!!

1.接收輸入字元串2.分析字元串中每個字元的ASCII碼,然後進行統計就好了

⑺ 用java編程統計用戶從鍵盤輸入的字元串中所包含的字母,數字和其他字元串的個數

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class _1 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

String zifuchuan = new String("");
int hanzishu = 0;
int zimu = 0;
int kongge = 0;
int shuzi = 0;
int qita = 0;
System.out.print("請輸入一行字元:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
zifuchuan = stdin.readLine();
byte[] bytes = zifuchuan.getBytes();
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] >= 65 && bytes[i] <= 90)
|| (bytes[i] >= 97 && bytes[i] <= 122))
zimu++;
else if (bytes[i] == 32)
kongge++;
else if (bytes[i] >= 48 && bytes[i] <= 57)
shuzi++;
else if (bytes[i] < 0)
hanzishu++;
else
qita++;
}
System.out.println("字元串所佔位元組個數為:" + bytes.length);
System.out.println("漢字個數為:" + hanzishu / 2);
System.out.println("英文字母個數為:" + zimu);
System.out.println("空格個數為:" + kongge);
System.out.println("數字個數為:" + shuzi);
System.out.println("其他字元個數為:" + qita);
}

}

⑻ java如何統計字元串中有幾個數字

String reg="\\d";
String str="a23saf3sdf2fsdf";
int count = (" " + str + " ").split(reg).length-1;
System.out.println(count);

⑼ 如何用java語言編寫「求一個字元串中的的字母,數字,空格的數目。」

public
class
StringNumber
{
private
String
str="
";
private
int
z=0;
private
int
k=0;
private
int
g=0;
public
StringNumber(String
str){
this.str=str;
}
public
void
strNumber(){
byte
buf[]=str.getBytes();
for(int
i=0;i<str.length();i++){
if(buf[i]=='
'){
z++;
}
if((buf[i]>='0')&&(buf[i]<='9')){
k++;
}
if(((buf[i]>='a')&&(buf[i]<='z'))||((buf[i]>='A')&&(buf[i]<='Z'))){
g++;
}
}
System.out.println("空格數是"+z);
System.out.println("數字個數"+k);
System.out.println("字母個數"+g);
}
public
static
void
main(String[]
args)
{
//
TODO:
在這添加你的代碼
StringNumber
strnumber
=
new
StringNumber("aaasdfdsfdsf
fdAADD3432
erew34");
strnumber.strNumber();
}
}

閱讀全文

與java字元串數字個數相關的資料

熱點內容
修改本地賬戶管理員文件夾 瀏覽:416
python爬蟲工程師招聘 瀏覽:283
小鵬p7聽音樂哪個app好 瀏覽:354
linux下的防火牆 瀏覽:954
凌達壓縮機美芝壓縮機 瀏覽:350
php後面代碼不執行 瀏覽:236
微我手機怎樣設置應用加密 瀏覽:202
條件加密 瀏覽:628
androidstudio設置中文 瀏覽:641
汽車換壓縮機能提升製冷 瀏覽:628
安卓開發配什麼電腦 瀏覽:607
linux下php模塊 瀏覽:78
阿里雲伺服器終端在哪裡 瀏覽:147
app紙有什麼用 瀏覽:224
cuteftp命令 瀏覽:507
最開始的編程語言是什麼 瀏覽:760
at遠程命令 瀏覽:493
雲伺服器哪家好點 瀏覽:215
android系統源碼閱讀 瀏覽:931
dumpjava分析工具 瀏覽:680