繁体   English   中英

如何计算文件中的单词长度? Java的

[英]How to count length of words in a File? Java

我正在尝试编写一个代码来计算文件中某个长度的单词数。

例如:

How are you?

会打印:

Proportion of 3-letter words: 100%  (3 words)

我想计算长度分别为1,2,3,4,5,6,7,8,9,10,11,12,13 +的单词

你能指导我吗?

我不是想找到单词的数量。 我已经能够使用这段代码了:

public static int WordCount() throws FileNotFoundException
{
    File file = new File("sample.txt");
    Scanner keyboard = new Scanner(new FileInputStream(file));
    int count=0;
    while(keyboard.hasNext())
    {
      keyboard.next();
      count++;
    }
    return count;
}

我想找到一定长度的单词

UPDATE

我写了以下代码:

public static int WordLengthCount() throws FileNotFoundException
{
  File file = new File("hello.txt");
  Scanner keyboard = new Scanner(new FileInputStream(file));
  int count5 = 0;
  int hell = 0; //This is just for the else command to compile

  while(keyboard.hasNext())
  {
    if ( keyboard.next().length() == 5 )
    {
      count5++;
      keyboard.next();
      return count5;
    }
  } return hell;
}

您可以使用length()方法计算字符串(单词)中的字符数。 从那以后,只需要将它保存在某个地方。 例如,在Map

public static Map<Integer, Integer> lengthCounts() throws FileNotFoundException
    Map<Integer, Integer> countMap = new HashMap<>();
    while(keyboard.hasNext())
    {
        String word = keyboard.next();
        int length = word.length();
        Integer currCount = countMap.get(length);
        if (currCount == null) {
            countMap.put (length, 1);
        else {
            countMap.put (length, currCount + 1);
        }
    }
    return countMap;
}

现在,您可以检查具有任何特定长度的单词数,甚至可以打印所有单词。

编辑:
如果您唯一需要的是一定长度的单词的百分比,您只需要两个计数器 - 一个用于该长度的单词,一个用于所有单词:

public static double lengthPercentage(int requiredLength) throws FileNotFoundException
    int allWords = 0;
    int requiredWords = 0;
    while(keyboard.hasNext())
    {
        String word = keyboard.next();
        int length = word.length();
        if (length == requiredLength) {
            ++requiredWords;
        }
        ++allWords;
    }
    // implicit assumption: there's at least on word in the file
    return ((double) requiredWords) / allWords;
}
File file = new File("sample.txt");
    Scanner keyboard = new Scanner(new FileInputStream(file));
    int count=0;
    while(keyboard.hasNext())
    {
      keyboard.next();
      // Use a hash map
      // Check the string length and add it to the hash map by checking it already exists. If already exists then get the actual value from hashmap and increment it by one and save it again to the map.

      count++;
    }

因此,您的最终输出将是一个字母字符串计数,两个字母字符串计数等地图。

其他答案很棒,但是如果你想在文件中找到特定长度的单词并且你不喜欢上面的答案,那么你也可以尝试REGEX。 你可以测试每个单词,然后用它做你想做的事。 如果你在每个长度的文件中寻找单词的数量,我认为上面的答案更好,但是如果你想要检测特定长度的单词,你可以使用.length()或下面的正则表达式。 在我看来,使用字符串.lenght()函数更好,但我只是给你一个替代答案和例子。

我将在下面举一个小例子。

public class Words{
    public static void main(String [] args){
        String [] words = {"Pizzaaa", "Pizza", "Party"};
        int fives = 0;
        for( String s : words){
            if(s.matches(".{5}")){
                5++;

            }
        }
       System.out.println(fives);
    }
}

或者更好的版本:

public class Words{
    public static void main(String [] args){
        String [] words = {"Pizzaaa", "Pizza", "Party"};
        int fives = 0;
        for( String s : words){
            if(s.length() == 5){
                5++;

            }
        }
       System.out.println(fives);
    }
}

编辑如下:演示如何在基于文件的循环中使用它

// other code needed
while(in.hasNext())
{
  String s = in.next();
  if(s.length() == 5)
      fives++;
}

例如,我在C:\\有内容的文本文件名为TextFile.txt

Ut porttitor libero sodales quam sagittis, id facilisis lectus semper.

和Java代码:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Example {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\TextFile.txt");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);    

        if (dis.available() != 0) {
            // Get the line.
            String s = dis.readLine();
            // Put words to array.
            String[] sParts = s.split(" ");
            // Initialize word longest length.
            int longestLength = 1;
            for (String strx : sParts) { // Go through each sPart, the next one is called strx
                // If the document has word longer than.
                if (longestLength < strx.length())
                    // Set new value for longest length.
                    longestLength = strx.length();
            }
            // Because array index from "0".
            int[] counts = new int[longestLength + 1];
            for (String str : sParts) {
                // Add one to the number of words that length has
                counts[str.length()] += 1;
            }
            // We use this type of loop since we need the length.
            for (int i = 1; i < counts.length; i++) {
                System.out.println(i + " letter words: " + counts[i]);
            }
        }
    }
}

// Result:
//        1 letter words: 0
//        2 letter words: 2
//        3 letter words: 0
//        4 letter words: 1
//        5 letter words: 0
//        6 letter words: 2
//        7 letter words: 2
//        8 letter words: 0
//        9 letter words: 3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM