如何找到字符串java的长度

时间:2014-02-03 08:55:55

标签: java string

我刚开始学习Java ......

我需要输入一个txt。充满单词,然后输出一个直方图,列出频率的长度。

egLength 0 1 2 3 4 //

频率0 20 6 1 0 //

    public static void main( String args[] ) throws Exception  {
         if(args.length == 0) {
             System.out.println("blablabla");
          }

        BufferedReader inFile = new BufferedReader ( new FileReader ( args[0] ) );
        String[] wordList = new String[10]; // store the words
        int[] histogram = new int[5];// frequency counters
        int wordCnt = 0;

        while (inFile.ready()) {

        String word = inFile.readLine();

            if ( word.length == wordCnt) {
                histogram[wordCnt]++;
            }

            if (wordCnt == wordList.length){
                wordList = doubleTheArray(wordList);
            }

        wordList[wordCnt++] = word;
        }

        inFile.close();

        wordList=trim(wordList); 

Q1:我不确定是否应将wordList=trim(wordList);放在inFile.close();

前面
        System.out.printf( "List contains %d Strings.\n",wordList.length);
        System.out.println("LEN\tFREQ");
        for ( int i = 0; i < histogram.length ; ++i) //
            System.out.printf( "%2d\t%d\n",i,histogram[i] );
    } // END MAIN

Q2:我无法获得histogram[i]。任何人都可以给我一个提示吗?

3 个答案:

答案 0 :(得分:0)

似乎修剪inFile.readLine()更有意义。

您能告诉我们您填充histogram数组的位置吗?

答案 1 :(得分:0)

我的建议是:

1.  Use a HashMap with (length, count) as key and value respectively..
2.  Read all the words from the file, for each word read, check the length.
3. if length of word already exists as key in the HashMap, increment the count value of hashMap else add the length as key and set its value (count ) to 1. This approach would simplify things..

答案 2 :(得分:0)

修复:

Q1)

while (true) {
    String word = inFile.readLine();
    if (word == null) {
        break
    }
    word = word.trim();

Q2)

如果histogram[i]是长度为i的所有字词的计数,那么

        if ( word.length == wordCnt) {
            histogram[wordCnt]++;
        }

应该是

        if (word.length() < histogram.length) { // Only limited length counted.
            histogram[word.length()]++;
        }

对于数组String[],有一个计数字段.length。对于String,有一种名为.length()的方法。 顺便说一下,字段和方法在类中可以具有相同的名称;并不是说它是好的风格,因为字段应该是实质性的(“长度”)或形容词(“空”)和方法动词(“isEmpty”,“getLength”)。


让我们跳进深处。

    int totalWordCount = 0;
    Map<String, Integer> histogram = new TreeMap<>(); // Word to count map.
    // Implement histogram as TreeMap, which is sorted by key.

    try (BufferedReader inFile = new BufferedReader(new FileReader(args[0]))) {
        for (;;) {
            String line = inFile.readLine();
            if (line == null) {
                break;
            }
            String[] words = line.split("\\W+"); // Split by non-word chars.
            for (String word : words) {
                if (word.isEmpty()) {
                    continue;
                }
                ++totalWordCount;
                Integer wordCount = histogram.get(word);
                int wc = wordCount == null ? 0 : wordCount.intValue();
                histogram.put(word, wc + 1);
            }
        }
    }

    System.out.println("Histogram:");
    for (Map.Entry<String, Integer> entry : histogram.entrySet()) {
        double frequency = ((double)entry.getValue().intValue()) / totalWordCount;
        System.out.printf("%-20s %1.6f%n", entry.getKey(), frequency);
    }
  1. 从单词到数字的地图将很好地完成。要使单词(键)排序,将其实现为TreeMap,否则通常采用HashMap。
  2. 从Java 7开始,构造try (DECLARATIONS) {将始终调用close()
  3. readLine产生没有行尾的行; null表示文件结束。
  4. 拆分可以用来挑选单词。 \\w将匹配单词char,\\W非单词char; postfix +一个或多个。当行以非单词char开头时,可能会发生空字符串。所以你需要跳过空字符串。