⑴ 用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中如何判斷一個字元串中含有字母或數字
比如:
public static void main(String args[]) {
boolean isNumber=false; //定義一個boolean值,用來表示是否包含數字
String str="aaasss8fff"; //假設有一個字元串
for(int i=0;i<str.length();i++){ //循環遍歷字元串
if(Character.isDigit(str.charAt(i))){ //用char包裝類中的判斷數字的方法判斷每一個字元
isNumber=true;
}
}
/*
* 循環完畢以後,如果isNumber為true,則代表字元串中包含數字,否則不包含
*/
}
⑶ 怎麼用java判斷字元含有有數字和字母
把字元存入一個數組裡面,然後再使用正則表達式判斷。
⑷ Java怎麼生成一個指定長度的字元串,且這個字元串必須包含大小寫字母和數字
這個應該可以達到你的要求。
---------------------------------------------------------------------------
public class StringLen {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer();
int len = 10;
if (len < 3) {
// 必須包含大小寫字母和數字
// 那這個字元串最小長度是3.
return;
}
int index = 0;
for (int i = 0; i < len; i++) {
char c = 'a';
int random = (int) (Math.random() * 1000);
if (i > 3) {
index = random % 3;
} else {
index = i;
}
switch (index) {
case 0:
buff.append((char) (97 + random % 26));
break;
case 1:
buff.append((char) (65 + random % 26));
break;
case 2:
buff.append((char) (48 + random % 10));
break;
}
}
System.out.println(buff.toString());
}
}
⑸ java 正則表達式 包含字母數字特殊字元
/(?!d{3}|[a-z]{3})([0-9a-z])(?!11)[0-9a-z]*/gi
我不明白你的第一條的意思
我只能不匹配 1. 3個連續字母或3個連續數字(4個連續也不能匹配)
2. 3個連續相同的字母或數字(其實1已經否定了2)
/***/gi gi是全局不區分大小寫匹配 ***是正則表達式
希望我這樣寫你能從中得到啟發
⑹ Java編程統計用戶從鍵盤輸入的字元串中所包含的字母,數字和其他字元的個數。 不要寫的太難,初學者水平!
importjava.util.Scanner;//導入Scanner類
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Scanners=newScanner(System.in);//實例化輸入流
Stringst=null;//定義字元串用於接收輸入
System.out.println("請輸入字元串:");
st=s.next();//完成對輸入的接收
char[]c=st.toCharArray();//將接收的字元串轉化為字元數組
intn=0;//用於數字字元計數
for(inti=0;i<c.length;i++){
if(c[i]>='0'&&c[i]<='9'){
n++;
}
}
intn1=0;//用於小寫字母字元計數
for(inti=0;i<c.length;i++){
if(c[i]>='a'&&c[i]<='z'){
n1++;
}
}
intn2=0;//用於大寫字母字元計數
for(inti=0;i<c.length;i++){
if(c[i]>='A'&&c[i]<='Z'){
n2++;
}
}
intn3=c.length-n-n1-n2;//排除已計數字元後的剩餘字元數
System.out.println("您輸入的字元串中數字的個數是"+n);
System.out.println("您輸入的字元串中小寫字母的個數是"+n1);
System.out.println("您輸入的字元串中大寫字母的個數是"+n2);
System.out.println("您輸入的字元串中其它字元的個數是"+n3);
}
}