Results 1 to 1 of 1
Thread: word syllable program help
- 11-16-2012, 01:28 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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
class:
Java 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; } }
Java 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()); } }
Similar Threads
-
I really need help explaining this Word Count program!
By rsp0413 in forum New To JavaReplies: 6Last Post: 04-25-2012, 10:17 PM -
trying to write a program for hangman word game
By durdanto in forum New To JavaReplies: 1Last Post: 02-12-2011, 03:53 AM -
a program that counts the number of each word in a sentence given by the user
By rambo126 in forum New To JavaReplies: 13Last Post: 10-31-2010, 09:42 PM -
Program that displays JAVA word pattern.
By SNFA in forum New To JavaReplies: 10Last Post: 10-08-2010, 11:55 PM -
Longest word in a program...
By hustlas4ever in forum New To JavaReplies: 5Last Post: 08-20-2010, 02:34 PM
Bookmarks