[英]How do you make java read and count the number of sentence input
樣本輸出:
輸入文章:我喜歡貓。 輸入錯誤! 您的文章必須至少包含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.");
}
差距很多,我不知道該如何去做。 你能幫忙嗎?
您可以使用稍微復雜的正則表達式進行拆分,並取其長度:
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);
請注意,這還會計算其他以句子結尾的字符,並拒絕其他顯然不是2個句子的輸入,例如:
這應該可以工作,並且如果您使用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;
}
}
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");
使用https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#countMatches(java.lang.String,%20java.lang.String)用於計算“。”的數量 出現在字符串中。
直接取自Javadocs的示例:
計算子字符串出現在較大的String中的次數。
空或空(“”)字符串輸入將返回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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.