我们java课作业写一小程序 我从网上找了一个 但是完全看不懂 求大神逐字帮我解释下 java基于Eclipse环境 50
packageTongji;importjava.util.*;importjava.util.regex.*;importjava.io.*;publicclassYi...
package Tongji;
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class Yinhe
{
public static void main(String args[]) throws IOException
{
BufferedReader buf=new BufferedReader(new FileReader("english.txt"));
System.out.println("Read under this dir English.txt");
StringBuffer sbuf=new StringBuffer();
String line=null;
while((line=buf.readLine())!=null)
{
sbuf.append(line);
}
buf.close();
Pattern expression=Pattern.compile("[a-za-z]+");
String string1=sbuf.toString().toLowerCase();
Matcher matcher=expression.matcher(string1);
TreeMap myTreeMap=new TreeMap();
int n=0;
Object word=null;
Object num=null;
while(matcher.find())
{
word=matcher.group();
n++;
if(myTreeMap.containsKey(word)){
num=myTreeMap.get(word);
Integer count=(Integer)num;
myTreeMap.put(word,new Integer(count.intValue()+1));
}
else
{
myTreeMap.put(word,new Integer(1));
}
}
System.out.println("统计分析如下:");
System.out.println("文章中单词总数"+n+"个");
System.out.println("具体的信息在目录的result.txt文件中");
BufferedWriter bufw=new BufferedWriter(new FileWriter("result.txt"));
Iterator iter=myTreeMap.keySet().iterator();
Object key=null;
while(iter.hasNext()){
key=iter.next();
bufw.write((String)key+":"+myTreeMap.get(key));
bufw.newLine();
}
bufw.write("english.txt中的单词总数"+n+"个");
bufw.newLine();
bufw.write("english.txt中不同单词"+myTreeMap.size()+"个");
bufw.close();
}
}
悬赏50 问题解决追加30 可以发邮箱 jiazhenwow@hotmail.com 展开
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class Yinhe
{
public static void main(String args[]) throws IOException
{
BufferedReader buf=new BufferedReader(new FileReader("english.txt"));
System.out.println("Read under this dir English.txt");
StringBuffer sbuf=new StringBuffer();
String line=null;
while((line=buf.readLine())!=null)
{
sbuf.append(line);
}
buf.close();
Pattern expression=Pattern.compile("[a-za-z]+");
String string1=sbuf.toString().toLowerCase();
Matcher matcher=expression.matcher(string1);
TreeMap myTreeMap=new TreeMap();
int n=0;
Object word=null;
Object num=null;
while(matcher.find())
{
word=matcher.group();
n++;
if(myTreeMap.containsKey(word)){
num=myTreeMap.get(word);
Integer count=(Integer)num;
myTreeMap.put(word,new Integer(count.intValue()+1));
}
else
{
myTreeMap.put(word,new Integer(1));
}
}
System.out.println("统计分析如下:");
System.out.println("文章中单词总数"+n+"个");
System.out.println("具体的信息在目录的result.txt文件中");
BufferedWriter bufw=new BufferedWriter(new FileWriter("result.txt"));
Iterator iter=myTreeMap.keySet().iterator();
Object key=null;
while(iter.hasNext()){
key=iter.next();
bufw.write((String)key+":"+myTreeMap.get(key));
bufw.newLine();
}
bufw.write("english.txt中的单词总数"+n+"个");
bufw.newLine();
bufw.write("english.txt中不同单词"+myTreeMap.size()+"个");
bufw.close();
}
}
悬赏50 问题解决追加30 可以发邮箱 jiazhenwow@hotmail.com 展开
展开全部
package Tongji;
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class Yinhe
{
public static void main(String args[]) throws IOException
{
//读取文件 english.txt
BufferedReader buf=new BufferedReader(new FileReader("english.txt"));
System.out.println("Read under this dir English.txt");
//文件信息保存在sbuf
StringBuffer sbuf=new StringBuffer();
String line=null;
while((line=buf.readLine())!=null)
{
sbuf.append(line);
}
buf.close();
//正则匹配是否是英文单词
Pattern expression=Pattern.compile("[a-za-z]+");
String string1=sbuf.toString().toLowerCase(); //转化为小写
Matcher matcher=expression.matcher(string1); //开始匹配
//创建一个TreeMap保存匹配结果
TreeMap myTreeMap=new TreeMap();
int n=0;
Object word=null;
Object num=null;
while(matcher.find()) //匹配到
{
word=matcher.group();
n++; //变量加1
if(myTreeMap.containsKey(word)){
num=myTreeMap.get(word);
Integer count=(Integer)num;
myTreeMap.put(word,new Integer(count.intValue()+1)); //提取匹配到的单词
}
else
{
myTreeMap.put(word,new Integer(1));
}
}
System.out.println("统计分析如下:");
System.out.println("文章中单词总数"+n+"个");
System.out.println("具体的信息在目录的result.txt文件中");
//下面的就不用说了,将结果保存到一个文件当中result.txt
BufferedWriter bufw=new BufferedWriter(new FileWriter("result.txt"));
Iterator iter=myTreeMap.keySet().iterator();
Object key=null;
while(iter.hasNext()){
key=iter.next();
bufw.write((String)key+":"+myTreeMap.get(key));
bufw.newLine();
}
bufw.write("english.txt中的单词总数"+n+"个");
bufw.newLine();
bufw.write("english.txt中不同单词"+myTreeMap.size()+"个");
bufw.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询