Java编写程序,用户输入一行字符串,要求计算字母,数字,空格以及其他字符的总数。

import java.io.*;
public class tongji{
public static void main(String[] args){
String s="";
int i,digit=0,space=0,letter=0,other=0;
System.out.println("Please input a string:");
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}catch(Exception e){}//输入字符串
char[]ch=s.toCharArray();//字符串转换成字符数组
for(i=0;i{
if(Character.isLetter(ch[i]))//判断是否为字母
letter++;
else if(Character.isSpaceChar(ch[i]))//判断是否为空格
space++;
else if(Character.isDigit(ch[i]))//判断是否为数字
digit++;
else
other++;
}
System.out.println("The number of letter is:"+letter);
System.out.println("The number of space is:"+space);
System.out.println("The number of digit is:"+digit);
System.out.println("Other is:"+other);
}
}

java 根据空格分割字符串 java字符串空格个数_git