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.