I am trying to count syllables and right now I’m running some test to see if I can find vowels and then go from there. However, my output is coming up as 0 and I fail to see where the error is.
import java.util.*;
import java.io.*;
public class Word{
private char[] letters;
private char[] vowels;
private int ct;
private int temp;
private int syllableCt;
private int iftest;
public Word(String[] words){
temp = 0;
ct = 0;
for (int i = 0;i<words.length;i++){
temp = countSyllables(words[i]);
}
}
public int countSyllables(String str){
char[] letters = str.toCharArray();
syllableCt = 0;
for (int i = 0; i<letters.length;i++){
if (isVowel(letters[i]))
syllableCt++;
System.out.println("" + letters[i] + "\n");
System.out.println("" + syllableCt + "\n");
}
return syllableCt;
}
public boolean isVowel(char ch){
int iftest = 0;
char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y'};
for (int i = 0;i<vowels.length;i++){
if (ch == i)
iftest = 1;
}
if (iftest == 1)
return true;
else
return false;
}
public static void main(String args[]){
String[] words;
words = new String[5];
words[0] = "dog";
words[1] = "moon";
words[2] = "syllables";
words[3] = "reddit";
words[4] = "3749832";
Word word = new Word(words);
System.exit(0);
}
}
There looks to be a mistake in the method which checks for the vowel
The check should be ch== vowels [i]. Also, the method and class can be greatly refactored, can’t help there as I don ‘t have access to a computer right now.
See if the method below works :