本文介绍了如何计算单词(不计算一系列特殊字符)使用scanner,循环,数组,if和else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算单词(不计算一系列特殊字符)使用scanner,循环,数组,if和else语句?

How to count words (do not count series of special char) using scanner, loop, array, if and else statement?

它尚未完成因为我不知道不知道怎么做!任何人都可以帮助我吗?

It's not finished yet because I don't know how! Could anyone help me?

import java.util.Scanner;

public class Main
{
  public static void main(String[] args)
  {
    System.out.println("Simple Java Word Count Program");

    String str1 = null;

    char ch;
    int sample = 0;
    int i;
    int j = 0;
    int k = 0;
    int wordCount = 1;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your quote:");
    str1 = in.nextLine();

    for (i = 0; i < str1.length(); i++)
    {
      if (str1.charAt(i) == ' ' && str1.charAt(i + 1) != ' ')
      {
        String[] arr = str1.split(" ");

        for (k = 0; k < ;)
        {
          for (j = 0; j < arr[i].length(); j++)
          {
            ch = arr[i].charAt(j);

            if (Character.isLetterOrDigit(ch))
            {
              wordCount++;
            }
          }
        }
      }
    }

    System.out.println("Word count is = " + wordCount);
  }
}


推荐答案

String string = "Simple Java Word Count Program";

String[] words = string.split("\\s+");

System.out.println("Words Count: " + words.length);

好东西现在你甚至可以使用 by 索引字符串中的第一个单词是 words [0] ,第二个单词是单词[ 1] 依此类推。

Good thing is now you can even use words by indexes. First word in the string would be words[0], second word would be words[1] and so on.

其他方法是计算空格数,然后单词数量将比1更多空格数(在理想世界中):

Other method is to count the number of spaces instead, and then number of words would be 1 more than the number of spaces (in an ideal world):

String string = "Simple Java Word Count Program";
string = string.trim();

int spaceCount = 0;
for(int i = string.length() - 1; i >= 0; i--)
{
  if(string.charAt(i) == ' ')
  {
    spaceCount++;
  }
}

System.out.println("Words Count: " + (spaceCount + 1));

如果两个单词之间有多个空格,则上面的 spaceCount 方法将失败。
为防止这种情况发生,请使用以下简单技术:

If there are multiple spaces between two words, then the above spaceCount method will fail.To prevent that from happening, use this simple technique:

String string = "  Simple  Java   Word  Count  Program ";
string = string.trim();

int spaceCount = 0;
boolean isLastCharSpace = false;
for(int i = string.length() - 1; i >= 0; i--)
{
  if(string.charAt(i) == ' ')
  {
    if(!isLastCharSpace)
    {
      spaceCount++;
      isLastCharSpace = true;
    }
  }
  else
  {
    isLastCharSpace = false;
  }
}

System.out.println("Words Count: " + (spaceCount + 1));

现在这将打印正确的字数,而不使用任何额外的内存,这与第一种方法不同,与第二种方法不同,不会落入单词之间多个连续空格的任何陷阱。

Now this will print the correct word count, without using any extra memory, unlike the first method, and without falling in any pitfall of multiple consecutive spaces between words, unlike the second method.

如果您的字符串中包含一系列特殊字符,并且附加了单词,那么就没有问题。如果它们没有附加单词,并且独立存在于您的字符串中,那么程序和实际答案的答案会有一些差异。人们甚至可以类似地编写代码以防止这种情况发生,并提供完全正确的答案。以下是这种方法的样子:

If you have series of special characters included in your string and if they are attached with words, then there will be no issue. If they are not attached with words, and are independently existing in your string, then there will be some discrepancy in the answers of program and real answer. One can even write code similarly to prevent such situation from happening, and to provide exactly correct answer. Here's how such method would look like:

String string = "  Simple  Java -  Word,  {Count  Program} ";
string = string.trim();

string = removeAllSpecialCharacters(string);

int spaceCount = 0;
boolean isLastCharSpace = false;
for (int i = string.length() - 1; i >= 0; i--)
{
  if (string.charAt(i) == ' ')
  {
    if (!isLastCharSpace)
    {
      spaceCount++;
      isLastCharSpace = true;
    }
  }
  else
  {
    isLastCharSpace = false;
  }
}

System.out.println("Words Count: " + (spaceCount + 1));

这里是 removeAllSpecialCharacters 方法:

private String removeAllSpecialCharacters(String string)
{
  StringBuilder stringBuilder = new StringBuilder();

  for (int i = 0; i < string.length(); i++)
  {
    char ch = string.charAt(i);

    // Allow only letters, digits, and spaces.
    if (Character.isLetterOrDigit(ch) || ch == ' ')
    {
      stringBuilder.append(ch);
    }
  }

  return stringBuilder.toString();
}

这篇关于如何计算单词(不计算一系列特殊字符)使用scanner,循环,数组,if和else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 08:18