Daniel Campbell
Daniel Campbell

Reputation: 71

Counting Syllables, loop issue

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);
    }
}

Upvotes: 1

Views: 2131

Answers (5)

rsp
rsp

Reputation: 23373

the line:

if (ch == i)

compares the character against the loop index, you prpbably meant:

if (ch == vowels[i])

instead of managing a flag named iftest you can return true when you find a vowel directly, as in (pseudo code):

loop over vowels {
    if character == vowel
        return true
}
return false

plus you probably don't want to reset syllableCt to 0 every call to countSyllables().

Upvotes: 0

Toomai
Toomai

Reputation: 4214

In isVowel, you're comparing the letter to i. You should be comparing it to vowels[i].

Upvotes: 0

Ivan Milles
Ivan Milles

Reputation: 314

Look at the assignment to 'temp' in your loop. If you want to calculate the total number of syllables in all words, you want to accumulate the number into temp like so: temp += countSyllables... If you want a separate count per word, you want temp to be an array, and fill that out inside your loop.

Right now you're only seeing the last syllable count, which is correct given that the last word does not have vowels.

Upvotes: 0

Akron
Akron

Reputation: 1533

instead of if(ch == i)

you want

if(ch == vowels[i])

in your isVowel() method

Upvotes: 0

Scorpion
Scorpion

Reputation: 3976

There looks to be a mistake in the method which checks for the vowel

     

if (ch == i)
          iftest = 1;

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 :

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 == vowels[i])
          return true;

  }
  
      return false;
}

Upvotes: 2

Related Questions