如何让java读取并计算句子输入的数量
样本输出:如何让java读取并计算句子输入的数量,java,validation,Java,Validation,样本输出: 进入文章:我喜欢猫。 输入错误!你的文章必须至少包含两个句子 请重试: 进入文章:我喜欢猫。我喜欢狗 你的文章有两个句子 这是我目前的代码: 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
进入文章:我喜欢猫。 输入错误!你的文章必须至少包含两个句子 请重试: 进入文章:我喜欢猫。我喜欢狗 你的文章有两个句子
这是我目前的代码:
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.");
}
有很多差距,我不知道如何进行这个项目。您能帮忙吗?用于计算字符串中出现“.”的次数
直接取自Javadocs的示例:
计算子字符串在较大字符串中出现的次数
null或空(“”)字符串输入返回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
Scanner Scanner=新的扫描仪(System.in);
系统输出打印(“输入文章:”);
字符串=scanner.nextLine();
while(文章拆分(“[.]”),长度<2){
System.out.println(“输入错误!您的文章必须至少包含两个句子,请重试:”);
文章=scanner.nextLine();
}
System.out.println(“你的文章包含“+文章.拆分([.]”)。长度+句子”);
您可以使用稍微复杂的正则表达式进行拆分,并采用以下长度:
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);
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入文章:”);
String散文=input.nextLine();
int sentenceCount=随笔.split(\\S(\\S*[.!?])+(?!\\d)”)。长度;
while(句子计数<2){
System.out.print(“输入错误!您的文章必须至少包含两个句子,请重试:”);
散文=输入。下一行();
句子计数=文章。拆分(\\S(\\S*[.!?])+(?!\\d)”)。长度;
}
System.out.printf(“你的文章包含%d个句子。”,句数);
请注意,这会额外计算其他句子终止字符,并拒绝显然不是两个句子的其他输入,例如:
- 我没有写一个李>
- WTF李>
- 圆周率是3.14159
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;
}
}
import java.util.Scanner;
公共类util{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(System.in);
整数计数=0;
字符串模式=“.”;
System.out.print(“输入您的句子:\n”);
String散文=input.nextLine();
//Java8支持这一点,否则需要实现方法
int howmany=发生次数(文章、模式);
System.out.print(“多少:+多少+”\n”);
而(!(文章.包含(模式))| |有多少<2){
System.out.print(“输入错误!您的文章必须至少包含两个句子,请重试:\n”);
散文=输入。下一行();
多少=发生次数(文章、模式);
}
int i=0;
//继续为模式调用indexOf。
while((i=文章.模式索引,i))!=-1){
//提前启动指数。
i+=pattern.length();
//增量计数。
计数++;
}
System.out.print(“你的文章包含“+count+”个句子”);
}
私有静态int countStringEvents(字符串文章、字符串模式){
//TODO自动生成的方法存根
int i=0;
整数计数=0;
//继续为模式调用indexOf。
while((i=文章.模式索引,i))!=-1){
//提前启动指数。
i+=pattern.length();
//增量计数。
计数++;
}
返回计数;
}
}
我不能把每个句子都写在一行吗?找到所有句号,数一数,然后做一个转换。@Gendarme我该怎么做?@plantstho Regex,只需匹配句号的模式,然后找到所有匹配的句号。将split()参数改为regexp并进行了简化,同时loopit现在查看了句子的数量。当然,我会投赞成票。另外,因为我想要运动精神徽章,我没有很多进步的机会。另外,老实说:)非常感谢,但是StringUtils.countMatches不起作用,我如何导入它?从()获取commons-lang3 jar并将其添加到您的项目中。或者,如果您正在使用maven,请将其添加到您的依赖项中。@plants直接从“Saying”中复制/粘贴感谢你,但它并没有回答这个问题。相反,把帮助你最多的答案投票出来。如果这些答案对你有帮助,请考虑用一种更有建设性的方式来感谢你——通过你自己的答案来回答你的同龄人在这里提出的问题。
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;
}
}