I'm writing a program that asks the user to enter keywords, and an essay, and then checks how many keywords were used in the essay.
Current output:
Enter keywords:
dog, cat
Enter essay:
i like cat
Exception in thread "main" java.lang.RuntimeException
Desired output:
Enter keywords:
dog, cat
Enter essay:
i like cat
1
Here's my code so far:
static int keywordsChecker(String shortEssay, String keywords) {
int count = 0;
for (int i = 0; i < keywords.length(); i++) {
String[] ary = keywords.split(",");
if (shortEssay.contains(ary)) {
count++;
}
System.out.println(count);
}
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter keywords: ");
String keyword = input.nextLine();
System.out.println("Enter essay: ");
String essay = input.nextLine();
keywordsChecker(essay, keyword);
}
static int keywordsChecker(String shortEssay, String keywords)
{
int count = 0;
String[] ary = keywords.split(",");
for (int i = 0; i < ary.length; i++) {
if (shortEssay.contains(ary[i])) {
count++;
}
}
return count;
}
Just you need to call this method and get your Keywords count. :)
This should be your approach:
keywords.split(",");.shortEssay string or not using shortEssay.contains(ary[index]).
count variable by 1.continue the loop.count variable.And so your keywordsChecker method looks like this:
static int keywordsChecker(String shortEssay, String keywords) {
int count = 0;
String[] ary = keywords.split(",");
for (int i = 0; i < ary.length; i++) {
if (shortEssay.contains(ary[i])) {
count++;
}
}
System.out.println(count);
return count;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With