Share on Google+Share on Google+

Display the word that begins with vowel using Java Program

Advertisement
In this section, you will learn how to separate the word that begins with vowels from the specified text.

Display the word that begins with vowel using Java Program

In this section, we are going to separate the word that begins with vowels from the specified text. As  the specified text consist of sentences that are terminated by either '.' or '?' or !. So first of all  we have replaced all these special characters using replaceAll() method. Then we have used StringTokenizer class that breaks the whole string into tokens. After that using the method startsWith() with the tokens, we have checked whether the token is started with vowel, if it is, then we have displayed that word.

Here is the code:

import java.util.*;
import java.util.regex.*;

public class Vowels {
	public static void main(String[] args) {
		String st = "hello! how are you? when are you coming? hope to see u soon.";
		String str = st.replaceAll("[?!.]", "");
		StringTokenizer tokenizer = new StringTokenizer(str);
		String s = "";
		while (tokenizer.hasMoreTokens()) {
			s = tokenizer.nextToken();
			if ((s.startsWith("a")) || (s.startsWith("e"))
					|| (s.startsWith("i")) || (s.startsWith("o"))
					|| (s.startsWith("u"))) {
				System.out.println(s);
			}
		}

	}
}

Output

are
are 
u

Advertisements

Advertisement

Posted on: November 17, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.