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]
# 1
What have you done so far?
CaptainMorgan08a at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 2

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

@akhila at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 3

Here's a start:

public int numberOfVowels(String word)

{

//loop through string and check if each character is a vowel

}

I'm not sure what you mean by "check if the word entered is a word." Could you post some code?

CaptainMorgan08a at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 4

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

}

}

TuringPesta at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 5
as of this moment i do not have my code on me, (at librairy) it is similiar to turingpest but in methods, i wll post it on tomarowThank you TuringPestMessage was edited by: @akhil
@akhila at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 6
> Thank you TuringPestGood luck explaining how it works to your teacher.
CaptainMorgan08a at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 7
> > Thank you TuringPest> > Good luck explaining how it works to your teacher.Just refer your teacher to the Book Of Mormon. It's all explained there.
Hippolytea at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 8
> > Thank you TuringPest> > Good luck explaining how it works to your teacher.This question is asked so many times they miight as well add a regex keyword \p{Vowel}, lol.
TuringPesta at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 9

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

@akhila at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...
# 10
When posting code please use code tags. See button labelled code.
floundera at 2007-7-12 17:56:22 > top of Java-index,Java Essentials,New To Java...