
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- Python Program that extract words starting with Vowel From A list
- Finding all words that start with an 'a' in Java
- Print all Subsequences of String which Start with Vowel and End with Consonant in C++
- How do we use an enum type with a constructor in Java?\n
- All possible binary numbers of length n with equal sum in both halves?
- How to extract values from an R data frame column that do not start and end with certain characters?
- How to extract words from a string vector in R?
- How to extract the first n values of all elements of a list in R?
- How to extract the last n values of all elements of a list in R?
- How do we define the start of a term in a definition list in HTML?
- How to extract the first n characters from a string using Java?
- How to extract the last n characters from a string using Java?
- How to extract the strings between two words in R?
- Can we override a start() method in Java?
- JavaScript code to extract the words in quotations

Advertisements