如何让java读取并计算句子输入的数量

时间:2016-12-15 16:15:28

标签: java validation

示例输出:

输入文章:我喜欢猫。 输入不正确!你的论文必须包含至少2个句子 再试一次:

输入文章:我喜欢猫。我喜欢狗。

你的文章包含2个句子。

到目前为止,这是我的代码:

System.out.print("Enter the essay: ");

String essay = input.nextLine();

do {
    System.out.print("Incorrect input! Your essay must contain at least 2 sentences, please try again: ");
    essay = input.nextLine();
} while (!(essay.contains(".")));

if (essay.contains(".")) {
    System.out.println("Your essay contains x sentences.");
}

有很多空白,我不知道如何进行这个计划。你能帮忙吗?

4 个答案:

答案 0 :(得分:4)

您可以使用稍微复杂的正则表达式进行拆分,并取长度:

Scanner input = new Scanner(System.in);

System.out.print("Enter the essay: ");

String essay = input.nextLine();
int sentenceCount = essay.split("\\S(\\s*[.!?])+(?!\\d)").length;

while (sentenceCount < 2) {
    System.out.print("Incorrect input! Your essay must contain at least 2 sentences, please try again: ");
    essay = input.nextLine();
    sentenceCount = essay.split("\\S(\\s*[.!?])+(?!\\d)").length;
}

System.out.printf("Your essay contains %d sentences.", sentenceCount);

Live demo on Ideone

请注意,这会额外计算其他句子终止字符,并拒绝显然不是2个句子的其他输入,例如:

  • ...
  • 我没写一个!!
  • WTF?!?!
  • Pi是3.14159。

答案 1 :(得分:1)

这应该可行,如果使用Java 8,则不需要countStringOccurrences方法。

import java.util.Scanner;

public class util {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        int count = 0;
        String pattern = ".";

        System.out.print("Enter your sentence:\n");
        String  essay = input.nextLine();

        //this is supported in Java 8 . Otherwise you need to implement method 
        int howmany = countStringOccurrences(essay, pattern);
        System.out.print("how many : " + howmany + "\n");

        while ( !(essay.contains(pattern) ) || howmany < 2) {

            System.out.print("Incorrect input! Your essay must contain at least 2 sentence, please try again:\n ");
            essay = input.nextLine();
            howmany = countStringOccurrences(essay, pattern);
        }

        int i = 0;
        // Keep calling indexOf for the pattern.
        while ((i = essay.indexOf(pattern, i)) != -1) {
            // Advance starting index.
            i += pattern.length();
            // Increment count.
            count++;
        }

        System.out.print("Your essay contains " + count + " sentences");   
    }

    private static int countStringOccurrences(String essay, String pattern) {
        // TODO Auto-generated method stub
        int i = 0;
        int count = 0;
        // Keep calling indexOf for the pattern.
        while ((i = essay.indexOf(pattern, i)) != -1) {
            // Advance starting index.
            i += pattern.length();
            // Increment count.
            count++;
        }

        return count;
    }

}

答案 2 :(得分:0)

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the essay: ");
String essay = scanner.nextLine();

while (essay.split("[.]").length < 2) {
    System.out.println("Incorrect input! Your essay must contain at least 2 sentences, please try again: ");
    essay = scanner.nextLine();
}
System.out.println("Your essay contains " + essay.split("[.]").length + " sentences");

答案 3 :(得分:-2)

使用https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#countMatches(java.lang.String,%20java.lang.String)来计算&#34;。&#34;的数量。在String中出现。

直接从Javadocs获取的示例:

  

计算子字符串在较大字符串中出现的次数。

     

null或空(&#34;&#34;)字符串输入返回0.

StringUtils.countMatches(null, *)       = 0
StringUtils.countMatches("", *)         = 0
StringUtils.countMatches("abba", null)  = 0
StringUtils.countMatches("abba", "")    = 0
StringUtils.countMatches("abba", "a")   = 2
StringUtils.countMatches("abba", "ab")  = 1
StringUtils.countMatches("abba", "xxx") = 0