im working on this one program that takes in a word/sentence and it returns the number of syllables that are in the word or sentence
the problem is though that it isnt returning the correct number of syllables
everytime i input something all i get is 0 syllables
i am an absolute noob and havent learned debugging yet so someone please help me
this method here will find out where all the words are by detecting spaces:
this method here is where a word is sent and syllables are found out:Code:public int wordBreaker ()
{
String brokenFromSentence = "";
int syllables = 0;
char characterAtCurrentIndex = '\u0000';
for (int indexNumber = 0; indexNumber <= word.length() - 1;indexNumber++)
{
characterAtCurrentIndex = word.charAt(indexNumber);
if (characterAtCurrentIndex != ' ' || characterAtCurrentIndex != word.charAt(word.length() - 1))
{
brokenFromSentence = brokenFromSentence.concat(new StringBuilder (characterAtCurrentIndex).toString());
}
else
{
syllables += countSyllables(brokenFromSentence);
brokenFromSentence = "";
}
}
return syllables;
}
this method here detects all the vowels:Code:public int countSyllables (String partOfAString)
{
char characterAtCurrentIndex = '\u0000';
char characterAtPreviousIndex = '\u0000';
int syllables = 0;
for (int indexNumber = 0; indexNumber < partOfAString.length()-1; indexNumber++)
{
characterAtCurrentIndex = partOfAString.charAt(indexNumber);
if (isVowel(characterAtCurrentIndex))
{
characterAtPreviousIndex = characterAtCurrentIndex;
}
if (isVowel(characterAtPreviousIndex) == true &&
isVowel(characterAtCurrentIndex) == false)
{
syllables++;
characterAtPreviousIndex = '\u0000';
}
}
vowelsExceptE = "AIOUYaiouy";
if (vowelsExceptE.indexOf(partOfAString.charAt(partOfAString.length()-1)) != -1)
{
syllables++;
}
if (syllables == 0)
{
syllables = 1;
}
return syllables;
}
Code:public static boolean isVowel (char ch)
{
vowels = "AEIOUYaeiouy";
if (vowels.indexOf(ch) != -1 )
{
return true;
}
return false;
}
this is the test driver we are supposed to use:
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.wordBreaker());
}
}
