Counting vowels ?
hi, i am new the JAVA programing language, right now i use the HSA console system, i was trying to create program that firsts, prompts user for a word, then tells if its a word or not (like it would be error if input was abc123, only letters would be accepted),
then counts how many vowels each word has using method programming
I have been struggling to complete this, i would really apprieciete any help offered
[433 byte] By [
@akhila] at [2007-11-27 6:31:30]

i have gotten to the point where to check if the word entered is a word
that is the main part i am having troubles at,
Also making a method to count vowels is confusing like i can make it count the word not the vowels like
(iam using the c console)
c.println (example.length());
Also thanks for replying
Message was edited by:
@akhil
Message was edited by:
@akhil
import java.util.Scanner;
import java.util.regex.*;
public class StringDemo{
public static void main(String[] args){
new StringDemo();
}
public StringDemo(){
Scanner inputer = new Scanner(System.in);
System.out.print("Enter Text: ");
String input = inputer.nextLine();
System.out.println("Your Input Was: " + input);
String vowels = input.replaceAll("[^aeiou]+", "");
System.out.println("" + vowels.length() + " Vowels: " + vowels);
String pattern = "[a-zA-Z\\s]+";
boolean onlyAlpha = Pattern.matches(pattern, input);
System.out.println("Is Only Alpha: " + onlyAlpha);
}
}
do
{
//2) prompt user to enter a word
//c.setTextBackgroundColor (Color.black);
c.println ("Please enter a word");
word = c.readString ();
boolean validLetter = isAWord (word); //method call
if (validLetter == true)
c.println ("\nThat is a Word");
else
c.println ("\nThat is NOT a Word");
int abc = countVowel (word);
c.println (" You got " + abc);
c.println ("Do you want to try again? (y/n)");
reply = c.readChar ();
Validreply = AskToContinue (reply);
}
while (Validreply == true);
c.println ("Good Bye");
} // main method
//3) Verify that the word contains only letters if not send an error message
public static boolean isAWord (String thisString)
{
<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
THIS IS THE PART I HAVE TROUBLE WITH
//String pattern = "[a-zA-Z\\s]+";
if (((thisString == "a") && (thisString == "z")))
//|| ((thisString >= 'A') && (thisString <= 'Z')))
//boolean onlyAlpha = pattern.matches(pattern, word);
//System.out.println("Is Only Alpha: " + onlyAlpha);
return true;
else
return false;
}
<<<<<<<<<<<>>>>>>>>>>>>>
//4) Determine the number of vowels in the word
public static int countVowel (String text)
{
int count = 0;
// change the string to lowercase
text = text.toLowerCase ();
for (int i = 0 ; i < text.length () ; i++)
{
char c = text.charAt (i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
count++;
}
}
return count;
}
//5) Ask if you wish to continue, if yes then loop the program
public static boolean AskToContinue (char answer)
{
if ((answer == 'y' || answer == 'Y'))
return true;
else
return false;
}
} // CountVowel class