1.Displaying vowels from the string
`package com.practise;
import java.util.Scanner;
public class PrintVowels {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the name to display only vowels");
String name=scanner.next();
for(int i=0;i<name.length();i++) {
if(name.charAt(i)=='A'||name.charAt(i)=='a'||name.charAt(i)=='E'||name.charAt(i)=='e'
||name.charAt(i)=='I'||name.charAt(i)=='i'||name.charAt(i)=='O'||name.charAt(i)=='o'
||name.charAt(i)=='U'||name.charAt(i)=='u') {
System.out.print(name.charAt(i));
}
}
}
}`
Output:
Enter the name to display only vowels
Toddayismonday
oaioa
- Finding the mobile number from String `package com.first;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FindMobileNumber {
public static void main(String[] args) {
String input="My mobile number is 9234561789";
Pattern pattern=Pattern.compile("[0-9]");
Matcher matcher=pattern.matcher(input);
while(matcher.find()) {
System.out.print(matcher.group());
}
}
}`
output:
9234561789
3.Finding mobile number from file
`package com.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FindMobilenumberfromFile {
public static void main(String[] args) throws IOException {
File folder=new File("C:/Users/OneDrive/Documents/Test");
folder.mkdir();
File note=new File("C:/Users/OneDrive/Documents/Test/test.txt");
note.createNewFile();
FileWriter pen = new FileWriter(note);
pen.write("A picture word essay refers to an essay that utilizes a combination of "
+ "images 68768 and written words to convey a message or tell a story. "
+ "It's a form of visual storytelling, where the pictures and the words "
+ "9879345634 work together to create a cohesive narrative.");
pen.close();
BufferedReader reader=new BufferedReader(new FileReader("C:/Users/"
+ "OneDrive/Documents/Test/test.txt"));
Pattern pattern=Pattern.compile("[6-9][0-9]{9}");
String input=reader.readLine();
while(input!=null) {
Matcher matcher=pattern.matcher(input);
while(matcher.find()) {
System.out.print(matcher.group());
}
input=reader.readLine();
System.out.println(" "); }
reader.close();
} }
`
output:
9879345634
Top comments (0)