
java源程序:统计文本中的小写字母,大写字母,数字和其他字符的个数。 5
展开全部
import java.io.FileInputStream;
public class Panduan {
public static void main(String[] args) {
Panduan panduan = new Panduan();
panduan.readFile("D://test.txt");
}
public void readFile(String fileName){
int upperChar = 0;
int lowerChar = 0;
int digit = 0;
int otherChar = 0;
try {
FileInputStream fis = new FileInputStream(fileName);
byte[] buff = new byte[1024];
while(fis.available()>0){
int len = fis.read(buff);
String s = new String(buff,0,len);
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(Character.isUpperCase(c)){
upperChar++;
} else if(Character.isLowerCase(c)){
lowerChar++;
} else if(Character.isDigit(c)){
digit++;
} else {
otherChar++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("大写字母:"+upperChar+" "+"小写字母"+lowerChar+" "+"数字:"+digit+" "+"其他字符:"+otherChar);
}
}
public class Panduan {
public static void main(String[] args) {
Panduan panduan = new Panduan();
panduan.readFile("D://test.txt");
}
public void readFile(String fileName){
int upperChar = 0;
int lowerChar = 0;
int digit = 0;
int otherChar = 0;
try {
FileInputStream fis = new FileInputStream(fileName);
byte[] buff = new byte[1024];
while(fis.available()>0){
int len = fis.read(buff);
String s = new String(buff,0,len);
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(Character.isUpperCase(c)){
upperChar++;
} else if(Character.isLowerCase(c)){
lowerChar++;
} else if(Character.isDigit(c)){
digit++;
} else {
otherChar++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("大写字母:"+upperChar+" "+"小写字母"+lowerChar+" "+"数字:"+digit+" "+"其他字符:"+otherChar);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询