Home Tutorial Java Core Display the word that begins with vowel using Java Program

 
 

Display the word that begins with vowel using Java Program
Posted on: November 17, 2009 at 12:00 AM
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

Related Tags for Display the word that begins with vowel using Java Program:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.