Question: enter a line of characters to calculate the numbers of English letters, spaces, numbers, and other characters.
1. Program analysis: Using the while statement, the condition is that the input character is not '\ n '.
Import java. util. Collections;
Public class ex7 {
Public static void main (String args [])
{
System. out. println ("Enter the string :");
Repeated scan = new partition (System. in );
String str = scan. next ();
String E1 = "[\ u4e00-\ u9fa5]";
String E2 = "[a-zA-Z]";
Int countH = 0;
Int countE = 0;
Char [] arrChar = str. toCharArray ();
String [] arrStr = new String [arrChar. length];
For (int I = 0; I <arrChar. length; I ++)
{
ArrStr [I] = String. valueOf (arrChar [I]);
}
For (String I: arrStr)
{
If (I. matches (E1 ))
{
CountH ++;
}
If (I. matches (E2 ))
{
CountE ++;
}
}
System. out. println ("number of Chinese characters" + countH );
System. out. println ("number of letters" + countE );
}
}
Example 2,
Count the numbers, letters, spaces, and other characters (including Chinese characters and punctuation marks) of the string, and calculate the total number of characters of the string.
Package util;
Public class CountStr {
/**
* There is a string that contains Chinese, English, and numeric characters. Count and print the number of characters.
* @ Author Mr. Liao
* @ Date 2013-09-04
* Text message sending platform, text message word count control query method
*/
Public static void main (String [] args ){
// String str = "adasf AAADFD: Chinese," 123 ";
// String str = "golden vest high-end commodity trading platform-2013 Grand love carat diamond project pre-sale, 18 months, starting from 30 thousand, 8%, please call Zhanheng private fortune for pre-purchase: 18611297979 ";
String str = "Zhan Heng Financial Management, established in Beijing in 2004, is one of the largest financial consulting institutions in China. Obtained an independent fund sales license issued by the state. It is one of the top 10 sponsors of the China Open tennis competition in 2013. Over the past 10 years, the company has accumulated a wealth of experience in providing comprehensive family financial planning for its customers. At present, it has more than 10000 medium-and high-end loyal customers and has allocated more than 20 billion RMB for the customer, ranking among the top three in the industry. ";
System. out. println ("[total number of characters 1]:" + countSum (str ));
System. out. println ("--------------------");
System. out. println ("[total number of characters 2]:" + countSum2 (str ));
System. out. println ("--------------------");
System. out. println ("[total number of characters 3]:" + str. length ());
}
Public static int countSum (String str ){
Int unicodeCount = 0;
Int szCount = 0;
Int zmCount = 0;
For (int I = 0; I <str. length (); I ++ ){
Char c = str. charAt (I );
If (c> = '0' & c <= '9 '){
SzCount ++;
} Else if (c> = 'A' & c <= 'Z') | (c> = 'A' & c <= 'Z ')){
ZmCount ++;
} Else {
UnicodeCount ++;
}
}
System. out. println ("Unicode:" + unicodeCount );
System. out. println ("number:" + szCount );
System. out. println ("Letter:" + zmCount );
Int sum = szCount + zmCount + unicodeCount;
Return sum;
}
Public static int countSum2 (String str ){
Int abccount = 0;
Int numcount = 0;
Int spacecount = 0;
Int othercount = 0;
Char [] B = str. toCharArray ();
For (int I = 0; I <B. length; I ++ ){
If (B [I]> = 'A' & B [I] <= 'Z' | B [I]> = 'A' & B [I] <= 'Z ') {
Abccount ++;
} Else if (B [I]> = '0' & B [I] <= '9 '){
Numcount ++;
} Else if (B [I] = ''){
Spacecount ++;
} Else {
Othercount ++;
}
}
Int sum = abccount + numcount + spacecount + othercount;
System. out. println ("The number of English letters in the string is:" + abccount );
System. out. println ("The number of digits contained in the string is:" + numcount );
System. out. println ("The number of spaces in the string is:" + spacecount );
System. out. println ("other characters contained in the string:" + othercount );
Return sum;
}
}
Console result:
Unicode: 132
Number: 20
Letter: 0
[Total characters: 1]: 152
--------------------
The number of English letters in the string is 0.
The number of digits contained in the string is 20.
The number of spaces contained in the string is: 0.
Other characters in the string: 132
[2]: 152 characters in total
--------------------
[3]: 152 characters in total