Question: I d like to ask an expert In java please Determine the number of syllables in a sentence with an arbitrary number of words. Here

Id like to ask an expert
In java please
Determine the number of syllables in a sentence with an arbitrary number of words.
Here are some rules you are required to use for determining the number of syllables in a word. There are other more complicated cases not described in the 4steps below. Ignore those cases for this assignment.
Step1)Do .toUpperCase()or .toLowerCase()to the input at the beginning so you have less comparisons
Step 2)Count the number of vowels including y (a,e,i,o ,u,y)
Step 3)Subtract 1for every instance of two vowels being next to each other.
Step 4)Subtract 1if there's a vowel at the end of the word (EXCEPT Y).
For example the word feline is 2syllables.
We determine that by taking the number of vowels (3),we check for any vowels next to each other (none),then we see if there are any vowels at the end of the word (1).The syllable count 3-1or 2.
Example
Input
I am tired
Output
4
Thank you
This is my current code
import java.util.Scanner;
public class Main {
public static void main(String[]args){
Scanner scnr =new Scanner(System.in);
String sentence ="I am tired";
int totalSyllables =countSyllablesInSentence(sentence);
System.out.println(totalSyllables);
}
public static int countSyllablesInSentence(String sentence){
//Convert the entire sentence to lowercase to reduce comparisons
sentence =sentence.toLowerCase();
String[]words =sentence.split("\\s+"); //Split the sentence into words
int totalSyllables =0;
for (String word : words){
totalSyllables +=countSyllablesInWord(word);
}
return totalSyllables;
}
public static int countSyllablesInWord(String word){
//Define vowels
String vowels ="aeiouy";
int syllableCount =0;
boolean lastCharWasVowel =false;
//Special case for one-letter words
if (word.length()==1&& vowels.indexOf(word.charAt(0))>=0){
return 1;
}
for (int i =0; i <word.length(); i++){
char currentChar =word.charAt(i);
if (vowels.indexOf(currentChar)>=0){
//Count the first vowel or a non-consecutive vowel
if (i ==0||!lastCharWasVowel){
syllableCount++;
}
lastCharWasVowel =true;
}else {
lastCharWasVowel =false;
}
}
//Handle silent 'e'(e.g.,in words like "made" or "taste")
if (word.endsWith("e")&& syllableCount >1&& vowels.indexOf(word.charAt(word.length()-2))<0){
syllableCount--;
}
//Ensure at least 1syllable for any word
return Math.max(syllableCount,1);
}
}
And these are the things I'm getting wrong
Input
this is hard
Your output
4
Expected output
3
Input
dylan is hungry
Your output
4
Expected output
5
Input
good food
Your output
4
Expected output
2
Input
hi
Your output
4
Expected output
1
Thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!