word syllable program help
i am working on a program where the user inputs word/s and my program is supposed to spit out the number of syllables the word/s has (ex: input-simplicity output-4) everything seems to work fine except when i enter in something like this, "fluttershy is awesome like that."
ive tried that input without the period but it keeps giving me 10 syllables for some reason when the expected output is 9
so can some1 help me fix that error :frusty::frusty::frusty:
class:
Code:
public class Word
{
String word;
char characterAtCurrentIndex;
char characterAtPreviousIndex;
String vowelsExceptE;
static String vowels;
public Word(String w)
{
word = w;
}
//counts the number of syllables in a word
public int countSyllables()
{
int syllables = 0;
for(int indexNumber = 0; indexNumber < word.length()-1; indexNumber++)
{
characterAtCurrentIndex = word.charAt(indexNumber);
//checks if the letter at the current index is a vowel
//if it is then assign it to previous index
if(isVowel(characterAtCurrentIndex))
{
characterAtPreviousIndex = characterAtCurrentIndex;
}
//checks if the previous index char is a vowel and the current index char is a consonant
//if it is then it is a syllable
if(isVowel(characterAtPreviousIndex) == true &&
isVowel(characterAtCurrentIndex) == false)
{
syllables++;
characterAtPreviousIndex = '\u0000'; //default value of a char
}
}
vowelsExceptE = "AIOUYaiouy";
//checks if the last char of the word is any vowel except e
//if it is then add 1 more to syllables
if (vowelsExceptE.indexOf(word.charAt(word.length()-1)) != -1)
{
syllables++;
}
//checks if syllable count is equal to 0
//then make syllable count equal to 1 because every word has atleast 1 syllable
if (syllables == 0)
{
syllables = 1;
}
return syllables;
}
//checks if char is a vowel
public static boolean isVowel(char ch)
{
vowels = "AEIOUYaeiouy";
if(vowels.indexOf(ch) != -1 )
{
return true;
}
return false;
}
}
driver:
Code:
import java.util.Scanner;
import java.io.*;
public class WordTester
{
public static void main(String [] args)throws IOException
{
Scanner reader = new Scanner(System.in);
System.out.print("Enter word/s: ");
String input = reader.nextLine();
Word w = new Word(input);
System.out.println("Word = " + input);
System.out.println("Syllables = " + w.countSyllables());
}
}