Reputation: 47
Sample output:
Enter the essay: I like cats. Incorrect input! Your essay must contain at least 2 sentences, please try again:
Enter the essay: I like cats. I like dogs.
Your essay contains 2 sentences.
This is my code so far:
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.");
}
There are many gaps and I don't know how to go about this program. Can you please help?
Upvotes: 1
Views: 1539
Reputation: 4694
You can use a somewhat complex regular expression to split, and take the length:
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);
Note that this additionally counts other sentence-terminating characters, and rejects other inputs that obviously aren't 2 sentences, such as:
Upvotes: 4
Reputation: 21
This should work, and if you use Java 8 you do not need countStringOccurrences method.
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;
}
}
Upvotes: 1
Reputation: 644
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");
Upvotes: 0