- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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?
To find a word starts with a vowel letter −
The split() method of the String class Split 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 so 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.
Following Java program prints all the words in this file which starts 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'||ch == ' ') { System.out.println(words[i]); } } } }
Output
originated idea exists a of on-line and at own of
Advertisements