
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we extract all the words start with a vowel and length equal to n in java?
In this article, we will learn to extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java. We can solve this problem using the following ways:
- Using the split() method of the String class.
- Using the StringTokenizer class.
- Using Regular Expressions.
Using the split() method of the String class
To extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java, we can use the following steps:
- The split() method of the String class splits the given string into an array of Strings using the split() method of the String class.
- In the for loop, traverse through each word of the obtained array.
- Get the first character of each word in the obtained array using the charAt() method.
- Verify if the character is equal to any of the vowels using the if loop, if s,o, print the word.
Example
Assume we have a text file with the following content:
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
The following Java program prints all the words in this file that start with a vowel letter:
import java.io.File; import java.util.Scanner; public class WordsStartWithVowel { public static String fileToString(String filePath) throws Exception { Scanner sc = new Scanner(new File(filePath)); StringBuffer sb = new StringBuffer(); String input = new String(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(input); } return sb.toString(); } public static void main(String args[]) throws Exception { String str = fileToString("D:\sample.txt"); String words[] = str.split(" "); for (int i = 0; i < words.length; i++) { char ch = words[i].charAt(0); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { System.out.println(words[i]); } } } }
Following is the output of the above program:
originated idea exists a of on-line and at own of
Using the StringTokenizer class
We will use the hasMoreTokens() method of the StringTokenizer class to check if there are more tokens in the string. If so, we will use the method called nextToken() to get the next token from the string.
To extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java, we can use the following steps:
- Read the source string.
- Invoke the StringTokenizer class by passing the source string.
- In the while loop, check if there are more tokens in the string using the hasMoreTokens() method.
- If so, get the next token from the string using the nextToken() method.
- Get the first character of each word in the obtained array using the charAt() method.
- Verify if the character is equal to any of the vowels using the if loop; if so, print the word.
- Else print "No words start with a vowel".
Example
Following is the Java program to extract all the words that start with a vowel and have a length equal to n from a given string using the StringTokenizer class:
import java.util.StringTokenizer; public class FindWordsStartWithVowel { public static void main(String[] args) { String str = "Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms."; StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens()) { String token = st.nextToken(); char ch = Character.toLowerCase(token.charAt(0)); if ("aeiou".indexOf(ch) >= 0) { System.out.println(token); } } } }
Following is the output of the above program:
originated idea exists a of on-line and at own of
Using Regular Expressions
We can also use regular expressions to extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java. Let's look at the steps to achieve this:
- Read the source string.
- Invoke the Pattern class by passing the source string.
- In the while loop, check if there are more tokens in the string using the find() method.
- Then, get the next token from the string using the group() method.
- Get the first character of each word in the obtained array using the charAt() method.
- Verify if the character is equal to any of the vowels using the if loop; if so, print the word.
Example
Following is the Java program to extract all the words that start with a vowel and have a length equal to n from a given string using regular expressions:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class FindWordsStartWithVowel { public static void main(String args[]) throws Exception { String str = "Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms."; Pattern pattern = Pattern.compile("\b[aeiouAEIOU]\w*\b"); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group()); } } }
Following is the output of the above program:
originated idea exists a of on-line and at own of