如何计算单词(包括空格)?

时间:2017-09-22 15:13:47

标签: java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MyFirstProject {

  public static void main(String argv[]) throws IOException {

    //This line will create a buffered reader that reads from the system input using an input stream reader
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    //input will contain the first line of input from the user   
    /**
     * This asks a question.
     */
    System.out.println("What is your input?");
    String input = bufferedReader.readLine();
    /**
     * This returns a valediction or the input.
     */
    if (input.equals("exit")) {
        System.out.println("GOODBYE!");
    }
    else {
        System.out.println(input);
    }


    }


/**
 * This counts the number of words (including spaces) in a string.
 * @param words The user input.
 * @return The number of words in a string.
 */
public static String numberOfWords(String words) {
  int count = 0;
  if (words.length() == 1) {
      count++;
      return (String.valueOf(count) + "word");
  }
  if (words.length() > 1) {
      for (int i = 0; i <= words.length(); i++) {
          if (words.charAt(i) == ' ') {
              count++;
          }
      }
      return (String.valueOf(count) + "words");
  }
  else {
      return (String.valueOf(count) + "words");
  }
}
}

我的问题非常简单。我很难理解为什么我的代码不会返回输入中的单词数。输出应该是这样的,例如,如果来自用户的控制台上的输入是&#34; hello world&#34;:

您的意见是什么?
你好世界
你好世界
3个字
1个空间
11个字符

4 个答案:

答案 0 :(得分:0)

您永远不会在主要方法中调用numberOfWords函数。

试试这个:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MyFirstProject {

  public static void main(String argv[]) throws IOException {

    //This line will create a buffered reader that reads from the system input using an input stream reader
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    //input will contain the first line of input from the user   
    /**
     * This asks a question.
     */
    System.out.println("What is your input?");
    String input = bufferedReader.readLine();
    /**
     * This returns a valediction or the input.
     */
    if (input.equals("exit")) {
        System.out.println("GOODBYE!");
    }
    else {
        //****** HERE'S IS CHANGE! ********
        System.out.println(numberOfWords(input));
    }


    }


/**
 * This counts the number of words (including spaces) in a string.
 * @param words The user input.
 * @return The number of words in a string.
 */
public static String numberOfWords(String words) {
  int count = 0;
  if (words.length() == 1) {
      count++;
      return (String.valueOf(count) + "word");
  }
  if (words.length() > 1) {
      for (int i = 0; i <= words.length(); i++) {
          if (words.charAt(i) == ' ') {
              count++;
          }
      }
      return (String.valueOf(count) + "words");
  }
  else {
      return (String.valueOf(count) + "words");
  }
}
}

答案 1 :(得分:0)

首先,为了能够得到函数numberOfWords的计算结果,你需要在输入中调用它,参见代码

下一步更喜欢Scanner而不是BufferedReader + InputStreamReader,语法更容易

public static void main(String argv[]) {

    Scanner sc = new Scanner(System.in);

    System.out.println("What is your input?");
    String input = sc.nextLine();                   // change to Scanner

    if (input.equals("exit")) {
        System.out.println("GOODBYE!");
    } else {
        System.out.println(input);
        System.out.println(numberOfWords(input));  // add method call
    }
}

我还提供了一种更简单的方法来实现你的目标:

public static String numberOfWords(String words) {
    int nbWord = words.split("\\s").length;
    int nbSpaces = nbWord - 1;
    return (nbWord + nbSpaces) + " words\n" + nbSpaces + " spaces\n" + words.length() + " characters";
}

words.split("\\s")拆分空格并创建一个单词数组,其长度是空格数

答案 2 :(得分:0)

在这里你可以使用分割功能

            String input = "Hello World";
    String w[]=input.split(" ");
    int words=w.length;
    int l=input.length();
    System.out.println("characters "+l);
    System.out.println("words "+words);
    System.out.println("spaces "+(words-1));

输出:字符11 字2 空间1

答案 3 :(得分:0)

使用拆分并将结果分配给数组会让您获得所需的所有信息,因为我们可以看到我先做修剪,因为我们假设白色空间只在单词之间。

words = words.trim();
      String tokens[] = words.split(" ");

下面是使用此方法的工作示例:

 words = words.trim();
      String tokens[] = words.split(" ");
      int chars =0;
      int wordsN =0;
      int whitespace = 0;
      for(String s : tokens){
          chars+=s.length();
          wordsN++;       
      }
      whitespace = wordsN-1;
      chars += whitespace;

      System.out.println(chars);
      System.out.println(wordsN);
      System.out.println(whitespace);