简体   繁体   English

计算句子的第一个单词中的字母数

[英]Count the number of letters in the first word of a sentence

I am in an introductory Java course at my university. 我在大学里参加入门Java课程。 For my assignment I have to write a program to display the number of 1 letter words in a sentence, 2 letter words in a sentence...etc. 对于我的作业,我必须编写一个程序以显示一个句子中的1个字母单词,一个句子中的2个字母单词的数量等。 The sentence is user inputted. 该句子是用户输入的。 I am supposed to use a loop and I am not allowed to use arrays. 我应该使用循环,并且不允许使用数组。

However for now just to start, I am just trying to find the number of letters in the first word of the sentence. 但是,从现在开始,我只是想在句子的第一个单词中查找字母数。 What I have gives me either an incorrect letter count or an error saying that the String index is out of range. 我所得到的信息要么是字母数不正确,要么是错误信息,指出String索引超出范围。

  Scanner myScanner = new Scanner(System.in);

  int letters = 1; 

  int wordCount1 = 1; 

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


  int space = userInput.indexOf(" "); // integer for a space character

  while (letters <= userInput.length()) {

    String firstWord = userInput.substring(0, space);
    if (firstWord.length() == 1)
      wordCount1 = 1;
    int nextSpace = space;
    userInput = userInput.substring(space);
  }
  System.out.print(wordCount1);

For example when I input "This is a sentence" it gives me "String index out of range: 4" Any help with this would be greatly appreciated. 例如,当我输入“这是一个句子”时,它给我“字符串索引超出范围:4”。对此的任何帮助将不胜感激。

Try with: 尝试:

int len = userInput.split(" ")[0].length();

This will give you an array of words splitted by whitespace, then just get the first position in array and finally get the length. 这将为您提供由空格分隔的单词数组,然后仅获得数组中的第一个位置,最后获得长度。

userInput.indexOf(" ");

This gives you the length of the first word without the use of an array. 这使您可以不使用数组而获得第一个单词的长度。

The StringIndexOutOfBoundsException is thrown because, since space is never updated, the code ends up trying to substring index 0 to 4 from a string with a length of 2 . 抛出StringIndexOutOfBoundsException是因为,由于从不更新space ,因此代码最终尝试从长度为2的字符串将索引0子字符串化为4

If userInput was printed in the while-loop, the output would be: 如果将userInput打印在while循环中,则输出为:

This is a sentence
 is a sentence
a sentence
ntence
ce

Then the StringIndexOutOfBounds is thrown. 然后抛出StringIndexOutOfBounds。

The way I would count every word from the sentence without the use of an array would be: 我不使用数组就可以计算句子中每个单词的方式是:

Scanner in = new Scanner(System.in);

System.out.print("Enter a sentence: ");
String input = in.nextLine();
in.close();

int wordCount = 0;

while (input.length() > 0) {
    wordCount++;
    int space = input.indexOf(" ");
    if (space == -1) { //Tests if there is no space left
        break;
    }
    input = input.substring(space + 1, input.length());
}

System.out.println("The number of word entered is: " + wordCount);

Your problem was that you haven't been updating space and letter. 您的问题是您尚未更新空格和字母。 See below your code with my little changes that should work fine. 看到下面的代码,我的一些小改动应该可以正常工作。

Scanner myScanner = new Scanner(System.in);

      int letters = 1; 

      int wordCount1 = 1;
      String firstWord = null;

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


      int space = -2; //= userInput.indexOf(" "); // integer for a space character

      while (letters <= userInput.length() && space != -1) {

        space = userInput.indexOf(" ");
        if (space != -1) 
            firstWord = userInput.substring(0, space);
        if (firstWord.length() == 1)
          wordCount1 = 1;
        userInput = userInput.substring(space + 1);
      }
      System.out.print(wordCount1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM