【问题标题】:Finding the length of a word in a string and finding how many words have that length. (Java)查找字符串中单词的长度并查找具有该长度的单词的数量。 (爪哇)
【发布时间】:2013-11-01 05:12:03
【问题描述】:

我需要一些帮助来查找单词的长度以及有多少单词具有该长度。例如,如果句子是"I am going to find some string lengths",则

输出将是

Number of String with length 1 is 1

Number of String with length 2 is 2

Number of String with length 4 is 2

Number of String with length 5 is 1

Number of String with length 6 is 1

Number of String with length 7 is 1

到目前为止,我得到了这个:

    String word;
    int wordlength;
    int count = 0;

    Scanner inFile = 
            new Scanner(new FileReader("C:\\Users\\Matt\\Documents\\WordSize.txt\\"));

    PrintWriter outFile = 
            new PrintWriter("wordsizes.out");

    while (inFile.hasNext())
    {
        word = inFile.next();

        wordlength = word.length();

        if (count >= 0)
            outFile.println(wordlength);

        count++;
    }

    outFile.close();
        }
}

这只是给出了每个单词的长度。

【问题讨论】:

  • 您的预期输出实际上没有意义,至少对我而言。无论如何,您面临的问题是什么?你还没有真正问过问题。
  • 问题和代码对我来说都没有意义! :(
  • 对不起各位!我试图找到一种方法来清楚地解释它,但让我再试一次。我需要找到字符串中每个单词的长度。找到每个单词的长度后,我需要列出每个长度有多少单词。所以如果有 3 个 5 个字母的单词和 2 个 6 个字母的单词,我需要它说出来。
  • @Matt,未删除答案。抱歉我没空。

标签: java string count word string-length


【解决方案1】:

你对我的付出没有任何意义。我下面的东西对你有用。

String str="I am going to find some string lengths";
  String[] arr=str.split(" ");
    Map<Integer,Integer> lengthMap=new HashMap<>();
    for(String i:arr){
        Integer val=lengthMap.get(i.length());
        if(val==null){
           val=0;
        }
        lengthMap.put(i.length(),val+1);
    }
    for(Map.Entry<Integer,Integer> i:lengthMap.entrySet()){
        System.out.println("Number of String with length "+i.getKey()+" is "+i.getValue());
    }

输出

  Number of String with length 1 is 1
  Number of String with length 2 is 2
  Number of String with length 4 is 2
  Number of String with length 5 is 1
  Number of String with length 6 is 1
  Number of String with length 7 is 1

【讨论】:

  • 是的!这是我想要的输出。不幸的是,我正在上一门初级 Java 课程,没有涵盖您用来解决我的问题的许多方法。但是你已经正确回答了。另外,我编辑了我的原始帖子以包含正确的输出。
【解决方案2】:

使用string.split() 函数实际上很容易。我写信是为了演示一个解决方案:

String inputStr = "I am going to find some string lengths";
String str[] = inputStr.split(" "); // split the strings: "I", "am", "going", etc

int maxSize = 0;

for(String s: str)   // finding the word with maximum size and take its length
  if(maxSize < s.length())
        maxSize = s.length();

int lCount[] = new int[maxSize+1]; 


for(String s1: str)
{
   lCount[s1.length()]++; // count each length's occurance
}

 for(int j=0; j<lCount.length;j++)
 {
     System.out.println("String length: "+j+" count: "+lCount[j]);
 }

【讨论】:

  • 当字符串为 String inputStr = "I am going to find some string length without anyerrors" 时会发生什么;。永远不要使用字符串的长度作为索引。这是一个非常非常危险的场景。
  • 我在 lCount 初始化下面放了一条评论,让他知道应该首先计算 lCount 的大小,因为我知道这是错误的
  • 那你为什么要发布一个错误的解决方案?!您可以自己完成size of MaxWord * str.length 并发布正确的解决方案。那不是更好吗?!
  • 是的,但 OP 不应该自己弄清楚吗?我只是打算演示而不是编写完整的工作解决方案,以便他可以复制和粘贴
猜你喜欢
  • 2021-04-27
  • 2015-05-21
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2016-05-28
  • 2018-11-25
  • 1970-01-01
相关资源
最近更新 更多