【问题标题】:how do I split a word in a string into two tokens if it contains a smaller word如果字符串中的单词包含较小的单词,如何将其拆分为两个标记
【发布时间】:2016-11-20 07:00:05
【问题描述】:

我想计算“the”在我根据用户输入创建的标记数组中出现的次数,并将其存储在名为“theCount”的变量中。我正在使用 for 循环遍历数组并使用 if 语句检查“the”。

我不允许使用正则表达式。

这是我目前所拥有的:

import java.util.*;

public class theCount
{
    public static void main (String[] args)
    {
        Scanner userInput = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String sentence = userInput.nextLine();

        String[] input =  sentence.split(" the");

        int theCount = 0;

        for (String token : input) {
            if (token == "the")
                theCount++;
                System.out.print("\n" + theCount); //I want it printed after
                                                   //iteration. 

        }




    }


}

【问题讨论】:

  • System.out.print...移出for loop括号,the之前的空格应该在split中删除
  • 如果split() 导致字符串被拆分,则您已经知道“the”已找到。只需打印input.length - 1 作为“the”的计数。不需要循环。

标签: java arrays if-statement token


【解决方案1】:

有几个问题:

  1. split(" the") 使用 " the" 作为分隔符并给出其余的单词。最好是使用空格分割。
  2. 使用token.equals("the") 而不是==

【讨论】:

    【解决方案2】:

    如果您想计算出现次数,请使用此示例代码:

    import java.util.*;
    public class theCount {
       public static void main(String[] args) {
           Scanner userInput = new Scanner(System.in);
           System.out.print("Enter a sentence: ");
           String sentence = userInput.nextLine();
           int theCount = sentence.length() - sentence.replace("the", "").length();
           System.out.print("Number of occurrence: " + theCount);
       }
    }
    

    【讨论】:

    • 它起作用了,我需要将 theCount 除以三,以获得单词在程序中出现的次数。由于您的方程式计算了字符串中出现“t”、“h”和“e”的次数。
    【解决方案3】:

    您可以将输入添加到数组列表,然后可以使用它。

    一种方法是从频率方法中获取计数。

    List<String> arrayList = new ArrayList<String>();
    arrayList.add("String"); //add all the words.
    
    Collections.frequency(arrayList, "the");
    

    第二种方法是从地图中获取计数。

    Map<String, Integer> map = new HashMap<String, Integer>();
    for(String s : arrayList){
            Integer count = map.get(s);
            map.put(s, count==null?1:count+1);
    } 
    //the below will give you the count of any word.
    map.get("the");
    

    【讨论】:

      【解决方案4】:

      从 Java 8 开始,您可以通过 stream api 来解决这个问题。这样会更简洁。以以下代码为例

      public static void main(String[] args) {
          String str = "The is the for THE and the the the the The The";
      
          long count = Stream.of(str.split(" "))
                  .filter(i -> i.equalsIgnoreCase("the"))
                  .count();
      
          System.out.println(count);
      }
      

      === 更新 ===

      public static void main(String[] args) {
      
          String str = " there these theology";
      
          long count = Stream.of(str.split(" "))
                  .map(String ::toLowerCase)
                  .filter(i -> i.contains("the"))
                  .count();
      
          System.out.println(count);
      }
      

      === 更新 ===

      即使一个字符串中有多个相同的子字符串,此解决方案也可以工作。

      public static void main(String[] args) {
      
          String str = " thesethefajfskfjthetheasdfjasdkfjthe";
          String findStr = "the";
      
          int count = 0;
          for (String s : str.split(" ")) {
              count += s.toLowerCase()
                      .split(findStr, -1).length - 1 ;
          }
      
          System.out.println(count);
      
      }
      

      This SO post 将帮助您了解,如何在单个字符串中查找所有子字符串。

      【讨论】:

      • 这对像这些神学这样的词有用吗?因为来自“the”的字母都在其中。
      • @Jason_Silva 我已经更新了我的答案。请参阅更新部分。这应该符合您的要求。为了更清楚,您是否还需要考虑那些the 是另一个单词的子字符串的单词?像kdfasfjTHEkfskf 这个词包含一个作为子字符串。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多