62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* java实现统计D:/aa.txt文件中出现的字母个数、数字个数、汉字个数、空格个数及行数
*
*/
public class StatisticalCharacters {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
String name = "D:/aa.txt"; //文件名
int num = 0; //数字数
int letter = 0; //字母数
int line = 0; //行数
int space = 0; //空格数
int word= 0; //汉字数
try{
File file=new File(name);
BufferedReader br= new BufferedReader(new FileReader(file));
String str = null;
while((str=br.readLine())!=null){
System.out.println(str);
line++;
num += countNumber(str);
letter += countLetter(str);
word += countChinese(str);
space += countSpace(str);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("数字数:"+num);
System.out.println("字母数"+letter);
System.out.println("汉字数"+word);
System.out.println("空格数"+space);
System.out.println("行数"+line);
}
/**
* 统计数字数
* @param str
* @return
*/
public static int countNumber(String str) {
int count = 0;
Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
/**
* 统计字母数
* @param str
* @return
*/
public static int countLetter(String str) {
int count = 0;
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
/**
* 统计汉字数
* @param str
* @return
*/
public static int countChinese(String str) {
int count = 0;
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
/**
* 统计空格数
* @param str
* @return
*/
public static int countSpace(String str) {
int count = 0;
Pattern p = Pattern.compile("\\s");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
}
for(int i=49;;i=i+70){
if(i%2==1&&i%3==2&&i%6==5){
System.out.println("至少"+i+"阶");
break;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<style type="text/css">
*{font-family:Verdana;font-size:96%;}
label {width:10em;float:left;}
label.error { float:none; color:red; padding-left:.5em; vertical-align:top;}
p{clear:both;}
.submit{margin-left:12em;}
em{font-weight:bold; color:#F00; padding-right:1em; vertical-align:top;}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#commentForm").validate({
rules: {
username:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
url:"url",
comment:"required"
}
});
});
</script>
<title>表单验证</title>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="#">
<fieldset>
<legend>表单验证</legend>
<p>
<label for="cusername">姓名</label><em>*</em>
<input id="cusername" name="username" size="25"/>
</p>
<p>
<label for="cemail">电子邮件</label><em>*</em>
<input id="cemail" name="email" size="25"/>
</p>
<p>
<label for="curl">网址</label><em> </em>
<input id="curl" name="url" size="25" value=""/>
</p>
<p>
<label for="ccomment">你的评论</label><em>*</em>
<textarea id="ccomment" name="comment" cols="22">
</textarea>
</p>
<p>
<input class="submit" type="submit" value="提交">
</p>
</fieldset>
</form>
</body>
</html>