【问题标题】:Space counter not functioning in Java空间计数器在 Java 中不起作用
【发布时间】:2018-10-27 20:39:26
【问题描述】:

我正在尝试在 java 中制作单词计数器。我正在尝试通过用空格分隔单词来计算单词。

我已经设法用修剪功能消除了句子前后的空格。但是,我无法针对用户在两个单词之间键入多个空格的情况进行调整。例如,到目前为止,在 hello 和 world 之间有多个空格的字符串“hello world”将输出大于 2 的字数。这是我迄今为止尝试解决此问题的代码。

public void countWord(){

    String tokens[] = userInput.trim().split(" ");
    int counter = tokens.length;

    for(int i = 0; i < tokens.length; ++i) {
        if(Objects.equals(" ", tokens[i])) {
            --counter;
        }
    }

    System.out.printf("Total word count is: %d", counter);
}

如您所见,我创建了一个字数计数整数,用于保存创建的标记数。然后我尝试查找仅包含“”的标记,然后将字数减少这些字符串的数量。但是,这并不能解决我的问题。

【问题讨论】:

  • Split 已经删除了所有空格,所以 for 循环是多余的。
  • 当你用空格分割时,你会得到一个没有它们的由空格分隔的单词。如果你想计算单词,那么只需调用 tokens.lebgth
  • 也许它不是标准的空格字符。尝试拆分所有空格split("\\s+")

标签: java


【解决方案1】:

尝试正则表达式拆分

userInput.split("\\s+");

【讨论】:

    【解决方案2】:

    您已经在空格上添加了split(),因此split() 返回的任何标记中都不会再有空格:

    通过拆分此字符串计算的字符串数组围绕给定正则表达式的匹配项

    (强调我的) 但是,如果您的 String 中有额外的空格,则会有额外的标记,这会影响长度。而是使用split("\\s+")。然后只返回Array 的长度,因为split() 已经返回所有以空格分隔的标记,即所有单词:

    System.out.printf("Total word count is: %d", tokens.length);
    

    将为测试打印5 String

    "Hello this   is a String"
    

    【讨论】:

    • split() 将返回 3 个标记,如果 2 个单词之间有两个空格。例如,“hello world”会给出 3 个标记,所以这不起作用。
    • 你的答案的第一部分不起作用,所以我建议你删除它。 :)
    • @KushagraGoyal 我编辑了我的答案,解释说如果有更多的空间,它会把东西扔掉
    • 这行得通,但我只是想知道 \\s+ 背后的含义是什么
    【解决方案3】:

    如果您打算计算单词,请尝试以下方法之一: 在其他人提到的那些中。

    1. 这里,这个解决方案使用StringTokenizer
    String words = "The Hello World     word counter by using     StringTokenizer";
    StringTokenizer st = new StringTokenizer(words);
    System.out.println(st.countTokens()); // => 8
    
    1. 通过这种方式,您可以利用正则表达式通过单词拆分字符串
    String words = "The Hello World     word counter by using     regex";
    int counter = words.split("\\w+").length;
    System.out.println(counter); // => 8
    
    1. Scanner 用于您自己的counter 方法:
    public static int counter(String words) {
        Scanner scanner = new Scanner(words);
        int count = 0;
        while(scanner.hasNext()) {
            count += 1;
            scanner.next();
        }
        return count;
    }
    

    如果你想按标题中所说的计算空格,可以使用Commons的StringUtils

    int count = StringUtils.countMatches("The Hello World     space counter by using     StringUtils", " ");
    System.out.println(count);
    

    或者,如果您使用 Spring,SpringUtils 也可供您使用。

    int count = StringUtils.countOccurrencesOf("The Hello World     space counter by using     Spring-StringUtils", " ");
    System.out.println(count);
    

    【讨论】:

      【解决方案4】:

      我认为您可以通过检查 tokens[i].equals("") 来轻松修复它。从而检查一个单词是否为空字符串。由于在使用多个空格时在 space 上拆分会在数组中创建空字符串对象,因此这应该可以工作。

      【讨论】:

        【解决方案5】:

        为什么不去掉所有出现的 2 个或更多相邻空格然后拆分:

        String tokens[] = userInput.trim().replaceAll("\\s+", " ").split(" ");
        

        【讨论】:

          猜你喜欢
          • 2018-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多