In: Computer Science
Language is Java
Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one.
The program should prompt for a word and respond with the estimated number of syllables in that word. A run of the program should look exactly like this:
Enter a word: hairy "hairy" has 2 syllables
Cannot use an array!!! Do NOT use continue or break!
My program accepts information fine, I'm just having trouble with the loop that counts how many syllables are in the string.
import java.util.*;
class Cnt
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int s1 = 0;
int s2 = 0;
String word;
boolean count = false;
System.out.println("Enter a word: ");
word = sc.nextLine();
for (int i = 0; i < word.length(); i++)
{
String lower = word.toLowerCase();
char newC = lower.charAt(i);
if ((newC == 'a' || newC == 'e' || newC == 'u' || newC == 'y'|| newC == 'i' || newC == 'o') && 'e' != word.length() - 1)
{
s1 = s1 + 1;
if (!count)
{
count = true;
s1++;
System.out.println("\""+ word+"\" has "+ s1 + " syllables");
}
else if('e' == word.charAt(word.length()-1))
{
s2 = s2 + 1;
System.out.println("\""+ word+"\" has "+ s2 + " syllables");
}
}
}
}}