I've been having a tough time trying to figure this one out. Wrote a couple of notes on my phone and ended up with this as a result:

import java.io.*;
import java.util.Scanner;

public class Assignment1
{
   //Returning if a character is a vowel or not.
   public static boolean isVowel(char c)
   {
      boolean bVowel = false;

      String temp = (c+"").toLowerCase();
        c = temp.charAt(0);

      /*
      Switch is checking each char setting the variable bVowel to true
      or false. After that, the method returns the variable bVowel to
      the main method.
      */
        switch(c)                                                                                                                                                                 //Used to check each character within the word to determine if
        {                                                                                                                                                                            //there is a vowel. If not the default statement will return a false.
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'y':
                bVowel = true;                                                                                                                                                    //If any of the characters within the string proves to be a vowel it will
            default:                                                                                                                                                                 //return true.
                bVowel = false;                                                                                                                                                //The opposite effect will happen if the character within the string is a constant.
        }
      return bVowel;
   }

   //Returning the number of syllables in a word.
   public static int countSyllables(String word)
   {
      int count = 0;

      for(int i = 0; i < word.length()-1; i++)
      {
         if(isVowel(word.charAt(i)))
         {
            if(isVowel(word.charAt(i)) == false && isVowel(word.charAt(i+1)) == true)
               count++;
         }
         if(i == word.length() && word.charAt(i) == 'e')
            count = count;    
      }

      return count;
   }

   public static void main(String[] args)
   {
      System.out.println("Welcome to my program. \n"
                            +"My name is Shanel Fowler from class CSC-210. \n"
                            +"I worked very hard to get this program to work, so I hope you enjoy.\n\n");


        /**
         *This section will attempt to create a file reader object that will allow
         *the words from the dictionary textfile to be read. Afterwards, the syllables
         *calculated will be stored into a text document called syllables.
         */
        String fileName = "dictionary.txt";
        try
        {
         /**
         *I've created a new file reader variable that can store each line
         *and a buffered reader to constantly update. Afterwards, a string name line
         *is placed there in order to temporarily store each value that is read
         *from the dictionary document.
         */
            FileReader inputSyllables = new FileReader(fileName);                   
            BufferedReader bufferSyllables = new BufferedReader(inputSyllables);                    

            String line;

            while((line = bufferSyllables.readLine()) != null)                                                                                                                                                              //If the line is not null the list will continue on.
            {                                                                                                                                                                                                                         //The buffer allows me to constantly update the words
                System.out.println(line+"                       Syllables: "+countSyllables(line));
                                                                                                                                                                                                                       //within the dictionary text file.
         }                                                                                                  
            bufferSyllables.close();
        }catch(Exception e)
        {
            System.out.println("There was an error while reading the file line by line."+ e.getMessage());
        }   

   }
}

I believe I am missing something in my countSyllable method. If anyone could give suggestions it would be much appreciated.

Check your switch, and remember that execution wuill drop through to the next clause unless you execute a break or a return...

Thanks for the response. I was able to fix that portion of my code then tried to edit my counting syllables method, but I know I'm missing another if-check.

//Returning the number of syllables in a word.
   public static int countSyllables(String word)
   {
      int count = 0;

      for(int i = 0; i < word.length()-1; i++)
      {
         if(isVowel(word.charAt(i)) == false && isVowel(word.charAt(i+1)) == true)
            count++;
         if(count == 0)
            count = 1;
         if(i == word.length()-1 && word.charAt(i) == 'e')
            count = count;           
      }
      return count;
   }

Words like zygote are being counted as one extra syllable than it actually is.

Edited 2 Years Ago by AbstractEden: Typo

Personally I have never seen a definition of "syllable" that was logically sound - the original is based on how people happen to speak the word. Do you have a definitive algorithm/specification for "syllable" in the context of this exercise?

I already turned it in, knowing I wasn't able to please the requirements but here's the rules:

  1. Each sequence of adjacent vowels a,e,i,o,u,y
  2. Except for the last e in a word, is a syllable.
  3. However if the algorithm yields count of 0, change it to 1.
   public static int syllables(String s) {
      final Pattern p = Pattern.compile("([ayeiou]+)");
      final String lowerCase = s.toLowerCase();
      final Matcher m = p.matcher(lowerCase);
      int count = 0;
      while (m.find())
         count++;

      if (lowerCase.endsWith("e"))
         count--;

      return count < 0 ? 1 : count;
   }
This question has already been answered. Start a new discussion instead.