要读取一个文本文件并对其中的每个单词进行统计,单词之间由空格分开的。(某公司的招聘题)   
  要求:读入一个文本文件(包括中文)   
  结果:显示这个文本文件中的所有单词及其出现的次数(按从高到低的顺序)

解决方案 »

  1.   

    我的这个东西有点错误亲大家帮我调试调试,共享一下:   
      //Demo3.java   
      import   java.io.*;   
      import   java.util.*;   
      class   StringInt     
      {   
      public   String[]   sg;   
      public   int[]   it;   
      StringInt()   
      {   
      }   
              StringInt(int   n)   
      {   
                sg=new   String[n];   
                it=new   int[n];   
              }   
      };   
        
      class   Demo3{   
      public   static   void   main(String   args[]){   
        
          try{       
                
            File     file=new     File("b.txt");       
                            BufferedReader     in=new     BufferedReader(new     FileReader(file));     
            BufferedWriter     out=new   BufferedWriter   (new   FileWriter   ("out.txt"));   
                            String     s="";   //定义的字符串为空,准备存放读取的文本     
                            StringBuffer     str=new     StringBuffer();       
                          
          
                            while((s=in.readLine())!=null){                                   
                str.append(s);//读取的文本放到字符串s中去   
                              }       
            String   str1=str.toString();//StringTokenizer中只能够用string不能用StringBuffer   
            StringTokenizer   st=new   StringTokenizer(str1,"   ");   
            int   n=st.countTokens();//求文本的长度   
        
            StringInt   SI=new   StringInt(n);//新建一个StringInt对象SI   
                          
                          SI.sg=str1.split("   ");//将读取的文本放入SI中的String数组中   
        
        for   (int   i=0;i<n   ;i++   )   
        {   
        SI.it[i]=1;       //将每个单词出现的次数赋初值为1   
        }   
        
          for(int   i=0;i<n-1;i++)   
        for   (int   j=i+1;j<n;j++   )   
        {   
          if((SI.sg[i].equals(SI.sg[j]))&&(SI.it[j]!=0))   
            {   
        SI.it[i]++;   //相同的单词个数合并,只在一个单词处记录,其它的都赋值为0   
                SI.it[j]--;   
        }   
                }   
                
        
            //用冒泡法进行排序   
              for(int   i=0;i<n;i++)   
      for(int   j=n-1;j>i;j--)   
                    {   
        if(SI.it[j]>SI.it[j-1])   
        {   int   temp=SI.it[j];   
                      SI.it[j]=SI.it[j-1];   
                      SI.it[j-1]=temp;   
        
                    String   Stemp=SI.sg[j];   
                      SI.sg[j]=SI.sg[j-1];   
                      SI.sg[j-1]=Stemp;   
          }   
            }   
        
        
              for   (int   i=0;i<n   ;i++)   
          {   
        if(SI.it[i]!=0){   
        
                System.out.println(SI.sg[i]+":"+SI.it[i]);   
          
        }   
        }   
      in.close();       
      out.close();   
              
                  }catch(Exception     e){       
                            System.out.println(e.toString());       
                    }       
                
              }   
            }
      

  2.   

    public class Word implements Comparable<Word>{
        private String word;
        private int occurrence;
        public Word(String w) {
            assert w==null;
            word=w;
        }
        public String getWord() {
            return this.word;
        }
        public int getOccurrence() {
            return this.occurrence;
        }
        public void increase(){
            occurrence++;
        }
        public boolean equals(Object another){
            if(another==null)
                return false;
            if(another instanceof Word){
                Word anotherWord=(Word)another;
                return anotherWord.equals(word);
            }else
                return false;
        }
        public int hashCode(){
            return word.hashCode();
        }
        public int compareTo(Word w) {
            if(occurrence < w.getOccurrence())
                return -1;
            else if(occurrence == w.getOccurrence())
                return 0;
            else
                return 1;
        }
    }
    //////////////////////////////////////////////////////////////////////////
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.StringTokenizer;public class WordParser {
        private File toBeParsed;
        public WordParser(File f) {
            assert f.exists()&&f.isFile();
            toBeParsed=f;
        }
        public ArrayList<Word> parse(){
            BufferedReader input=null;
            try {
                ArrayList<Word> words=new ArrayList<Word>();
                input = new BufferedReader(new FileReader(toBeParsed));
                String line;
                while((line=input.readLine())!=null){
                    StringTokenizer tokenizer=new StringTokenizer(line);
                    while(tokenizer.hasMoreTokens()){
                        String token=tokenizer.nextToken();
                        Word word=new Word(token);
                        int index=words.indexOf(word);
                        if(index==-1){
                            word.increase();
                            words.add(word);
                        }else
                            words.get(index).increase();
                    }
                }
                input.close();
                Collections.sort(words);
                return words;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }finally{
                if(input!=null){
                    try{
                        input.close();
                    }catch(IOException e){
                    }
                }
            }
        }
        
        public static void main(String[]args){
            WordParser parser=new WordParser(new File(args[0]));
            ArrayList<Word> words=parser.parse();
            for(Word word:words)
                System.out.println(word.getWord()+":"+word.getOccurrence());
        }
    }
      

  3.   

    public class Word implements Comparable<Word>{
        private String word;
        private int occurrence;
        public Word(String w) {
            assert w==null;
            word=w;
        }
        public String getWord() {
            return this.word;
        }
        public int getOccurrence() {
            return this.occurrence;
        }
        public void increase(){
            occurrence++;
        }
        public boolean equals(Object another){
            if(another==null)
                return false;
            if(another instanceof Word){
                Word anotherWord=(Word)another;
                return anotherWord.equals(word);
            }else
                return false;
        }
        public int hashCode(){
            return word.hashCode();
        }
        public int compareTo(Word w) {
            if(occurrence < w.getOccurrence())
                return -1;
            else if(occurrence == w.getOccurrence())
                return 0;
            else
                return 1;
        }
    }
    //////////////////////////////////////////////////////////////////////////
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.StringTokenizer;public class WordParser {
        private File toBeParsed;
        public WordParser(File f) {
            assert f.exists()&&f.isFile();
            toBeParsed=f;
        }
        public ArrayList<Word> parse(){
            BufferedReader input=null;
            try {
                ArrayList<Word> words=new ArrayList<Word>();
                input = new BufferedReader(new FileReader(toBeParsed));
                String line;
                while((line=input.readLine())!=null){
                    StringTokenizer tokenizer=new StringTokenizer(line);
                    while(tokenizer.hasMoreTokens()){
                        String token=tokenizer.nextToken();
                        Word word=new Word(token);
                        int index=words.indexOf(word);
                        if(index==-1){
                            word.increase();
                            words.add(word);
                        }else
                            words.get(index).increase();
                    }
                }
                input.close();
                Collections.sort(words);
                return words;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }finally{
                if(input!=null){
                    try{
                        input.close();
                    }catch(IOException e){
                    }
                }
            }
        }
        
        public static void main(String[]args){
            WordParser parser=new WordParser(new File(args[0]));
            ArrayList<Word> words=parser.parse();
            for(Word word:words)
                System.out.println(word.getWord()+":"+word.getOccurrence());
        }
    }public class Word implements Comparable<Word>{
        private String word;
        private int occurrence;
        public Word(String w) {
            assert w==null;
            word=w;
        }
        public String getWord() {
            return this.word;
        }
        public int getOccurrence() {
            return this.occurrence;
        }
        public void increase(){
            occurrence++;
        }
        public boolean equals(Object another){
            if(another==null)
                return false;
            if(another instanceof Word){
                Word anotherWord=(Word)another;
                return anotherWord.equals(word);
            }else
                return false;
        }
        public int hashCode(){
            return word.hashCode();
        }
        public int compareTo(Word w) {
            if(occurrence < w.getOccurrence())
                return -1;
            else if(occurrence == w.getOccurrence())
                return 0;
            else
                return 1;
        }
    }
    //////////////////////////////////////////////////////////////////////////
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.StringTokenizer;public class WordParser {
        private File toBeParsed;
        public WordParser(File f) {
            assert f.exists()&&f.isFile();
            toBeParsed=f;
        }
        public ArrayList<Word> parse(){
            BufferedReader input=null;
            try {
                ArrayList<Word> words=new ArrayList<Word>();
                input = new BufferedReader(new FileReader(toBeParsed));
                String line;
                while((line=input.readLine())!=null){
                    StringTokenizer tokenizer=new StringTokenizer(line);
                    while(tokenizer.hasMoreTokens()){
                        String token=tokenizer.nextToken();
                        Word word=new Word(token);
                        int index=words.indexOf(word);
                        if(index==-1){
                            word.increase();
                            words.add(word);
                        }else
                            words.get(index).increase();
                    }
                }
                input.close();
                Collections.sort(words);
                return words;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }finally{
                if(input!=null){
                    try{
                        input.close();
                    }catch(IOException e){
                    }
                }
            }
        }
        
        public static void main(String[]args){
            WordParser parser=new WordParser(new File(args[0]));
            ArrayList<Word> words=parser.parse();
            for(Word word:words)
                System.out.println(word.getWord()+":"+word.getOccurrence());
        }
    }
      

  4.   

    小家伙你在干嘛呢?
    把我的code吭哧喂了两下干嘛呢?
      

  5.   

    不好意思,那个Word的构造函数中的assert声明写错了,应该是:
    public Word(String w) {
    assert w!=null;
    word=w;
    }
      

  6.   

    还有个小错误,Word的equals方法改成:
    public boolean equals(Object another){
    if(another==null)
    return false;
    if(another instanceof Word){
    Word anotherWord=(Word)another;
    return anotherWord.getWord().equals(word);
    }else
    return false;
    }