使用 StreamTokenizer () 、 HashMap() 、 HashSet()

import java.io.*;

import java.util.*;

class A {

    public static void main(String args[]) throws Exception {

        Console con = System.console();

        String str;

        int i=0;

  

        HashMap map = new HashMap();

        HashSet set = new HashSet();

 

        System.out.println("Enter File Name : ");

        str = con.readLine();

        File f = new File(str);

        f.createNewFile();

        

        FileInputStream fis = new FileInputStream(str);

        StreamTokenizer st = new StreamTokenizer(fis);

        while(st.nextToken()!=StreamTokenizer.TT_EOF) {

         String s;

    

         switch(st.ttype) {

             case StreamTokenizer.TT_NUMBER:  s = st.nval+"";

             break;

              case StreamTokenizer.TT_WORD:    s = st.sval;

               break;

                default: s = ""+((char)st.ttype);

            }



            map.put(i+"",s);

            set.add(s);

            i++;

        }

 

        Iterator iter = set.iterator();

        System.out.println("Frequency Of Words :");

        while(iter.hasNext()) {

          String word;

          int count=0;

          word=(String)iter.next();

          

            for(int j=0; j<i ; j++) {

             String word2;

              word2=(String)map.get(j+"");

               if(word.equals(word2))

                count++;

            }

            System.out.println(" WORD : "+ word+" = "+count);

        }

        System.out.println("Total Words In Files: "+i);

    }

}

在此代码中,首先我已经创建了一个包含以下数据的文本文件:


@Hello Hii World#* c++ java salesforce


这段代码的输出是:


**词频:


字:# = 1


字:@ = 1


字:c = 1


字:销售人员 = 1


字:* = 1


字:这= 1


字:+ = 2


字:java = 1


字:世界 = 1


单词:你好 = 1


文件总字数:11**


我无法找到为什么这将 c++ 显示为单独的单词。我想在输出中将 c++ 组合为一个单词


梦里花落0921
浏览 97回答 1
1回答

holdtom

你可以这样做&nbsp; &nbsp; // Create the file at path specified in the String str&nbsp; &nbsp; // ...&nbsp; &nbsp; HashMap<String, Integer> map = new HashMap<>();&nbsp; &nbsp; InputStream fis = new FileInputStream(str);&nbsp; &nbsp; Reader bufferedReader = new BufferedReader(new InputStreamReader(fis));&nbsp; &nbsp; StreamTokenizer st = new StreamTokenizer(bufferedReader);&nbsp; &nbsp; st.wordChars('+', '+');&nbsp; &nbsp; while(st.nextToken() != StreamTokenizer.TT_EOF) {&nbsp; &nbsp; &nbsp; &nbsp; String s;&nbsp; &nbsp; &nbsp; &nbsp; switch(st.ttype) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case StreamTokenizer.TT_NUMBER:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = String.valueOf(st.nval);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case StreamTokenizer.TT_WORD:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = st.sval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = String.valueOf((char)st.ttype);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Integer val = map.get(s);&nbsp; &nbsp; &nbsp; &nbsp; if(val == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val = 1;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val++;&nbsp; &nbsp; &nbsp; &nbsp; map.put(s, val);&nbsp; &nbsp; }&nbsp; &nbsp; Set<String> keySet = map.keySet();&nbsp; &nbsp; Iterator<String> iter = keySet.iterator();&nbsp; &nbsp; System.out.println("Frequency Of Words :");&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; while(iter.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; String word = iter.next();&nbsp; &nbsp; &nbsp; &nbsp; int count = map.get(word);&nbsp; &nbsp; &nbsp; &nbsp; sum += count;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" WORD : " + word + " = " + count);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Total Words In Files: " + sum);请注意,我使用泛型而不是原始版本的 HashMap 和迭代器更新了您的代码。此外,您用于 StreamTokenizer 的构造函数已被弃用。使用 map 和 set 是没有用的,因为您可以使用 .keySet() 方法迭代 map 的键集。该映射现在从 String(单词)变为 Integer(单词数)。无论如何,关于您所做的示例,我认为简单的拆分方法会更合适。有关 StreamTokenizer 的 wordChars 方法的更多信息,您可以查看#wordChars(int, int)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java