这是一个程序设计题,题目为:
给定一篇中英文文档,统计中文和英文单词的频度和最长的单词。说明如下:
a.频度=单词在文档中出现的次数/文档总单词数;
b.单词以空格分开 中文单词是以单词的第一个字符为中文字符,英文单词是单词的第一个字符是英文字符;
c.最长的单词如果有多个,就打印所有的这些单词import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;//第一题
public class Words { /**
* @param args
*/
public void test() throws Exception
{
String[][] w=null; BufferedReader br = new BufferedReader(new FileReader("e:/words.txt"));
String Line=null;//空引用
int i=0;//文章的总行数
while((Line=br.readLine())!=null)//读入内存
{
w[i]=Line.split("\\s+");//这里出现NullPointerException异常,我调试不出来 i++;
}//while
br.close();
String[][] a=this.countWords(w);
int f=countAll(w);
wordsF(a,f);
countLong(w);
}//test()
private void wordsF(String[][]a,int f)//计算词频
{
double d=0d;
for(int m=0;m<a.length;m++)
{
d=(double)((a[m][1].lastIndexOf("1")+1)/f);
System.out.println(a[m][0]+" "+d);
}
}//wordsF
private String[][] countWords(String[][] w)//计算每个单词的个数
{
String[][] a=null;//a[单词][个数]
int b=0;
for(int m=0;m<w.length;m++)
{
for(int n=0;n<w.length;n++)
{
dis(a,b,w[m][n]);
a[b][0]=w[m][n];
}//for(n)
}//for(m)
return a;
}//countWords
private void dis(String[][] a,int b,String bb)//判断是否相同
{
for(int i=0;i<a.length;i++)
{
if(a[i][0].equals(bb))
{
a[i][1]+="1";
}//if
else
{
b++;
}//else
}//for(i)
}//dis
private int countAll(String[][] w)//计算单词总个数
{
int x=0;//所求的总数
for(int m=0;m<w.length;m++)
{
for(int n=0;n<w[m].length;n++)
{
x+=n;
}//for(n)
}//for(m)
return x;
}//countAll
private void countLong(String[][] a)//打印最长的单词
{
String xx=null;
int n=0;
for(int m=0;m<a.length;m++)
{
if(a[m][1].lastIndexOf("1")+1>n)
{
n=a[m][1].lastIndexOf("1")+1;
xx=a[m][0];
}
}//for(m)
System.out.println(xx);
}//countLong
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
new Words().test();
}}
给定一篇中英文文档,统计中文和英文单词的频度和最长的单词。说明如下:
a.频度=单词在文档中出现的次数/文档总单词数;
b.单词以空格分开 中文单词是以单词的第一个字符为中文字符,英文单词是单词的第一个字符是英文字符;
c.最长的单词如果有多个,就打印所有的这些单词import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;//第一题
public class Words { /**
* @param args
*/
public void test() throws Exception
{
String[][] w=null; BufferedReader br = new BufferedReader(new FileReader("e:/words.txt"));
String Line=null;//空引用
int i=0;//文章的总行数
while((Line=br.readLine())!=null)//读入内存
{
w[i]=Line.split("\\s+");//这里出现NullPointerException异常,我调试不出来 i++;
}//while
br.close();
String[][] a=this.countWords(w);
int f=countAll(w);
wordsF(a,f);
countLong(w);
}//test()
private void wordsF(String[][]a,int f)//计算词频
{
double d=0d;
for(int m=0;m<a.length;m++)
{
d=(double)((a[m][1].lastIndexOf("1")+1)/f);
System.out.println(a[m][0]+" "+d);
}
}//wordsF
private String[][] countWords(String[][] w)//计算每个单词的个数
{
String[][] a=null;//a[单词][个数]
int b=0;
for(int m=0;m<w.length;m++)
{
for(int n=0;n<w.length;n++)
{
dis(a,b,w[m][n]);
a[b][0]=w[m][n];
}//for(n)
}//for(m)
return a;
}//countWords
private void dis(String[][] a,int b,String bb)//判断是否相同
{
for(int i=0;i<a.length;i++)
{
if(a[i][0].equals(bb))
{
a[i][1]+="1";
}//if
else
{
b++;
}//else
}//for(i)
}//dis
private int countAll(String[][] w)//计算单词总个数
{
int x=0;//所求的总数
for(int m=0;m<w.length;m++)
{
for(int n=0;n<w[m].length;n++)
{
x+=n;
}//for(n)
}//for(m)
return x;
}//countAll
private void countLong(String[][] a)//打印最长的单词
{
String xx=null;
int n=0;
for(int m=0;m<a.length;m++)
{
if(a[m][1].lastIndexOf("1")+1>n)
{
n=a[m][1].lastIndexOf("1")+1;
xx=a[m][0];
}
}//for(m)
System.out.println(xx);
}//countLong
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
new Words().test();
}}
必须要给w赋值才可以呀,不然
肯定空指针