Programming Problem: 1 problem You will be asked to write a Java program. The pr
ID: 3768059 • Letter: P
Question
Programming Problem: 1 problem You will be asked to write a Java program.
The program involves working on a problem that covers:
i. Java classes ii. Java class relationships
iii. Using the Scanner, File, and IOException for IO operation
iv. An example of programming problem is given below.
Write a Java program that performs the following. The operation must be done by instance methods. a. Read data from an input file that consits of string type. b. Store those data into an ArrayList or a simple stirng array. c. Display those stirng data with more than three vowels.
Explanation / Answer
Code :
import java.io.*;
import java.util.*;
public class fread{
public static void main(String[] args){
File file = new File("file.txt");
try {
Scanner sc = new Scanner(file);
ArrayList<String> al = new ArrayList<String>();
while (sc.hasNextLine()) {
String s = sc.next();
al.add(s);
System.out.println(s);
}
int l=al.size();
for(int i=0;i<l;i++){
int count=0;
String x=al.get(i);
int len=x.length();
for(int j=0;j<len;j++){
if(x.charAt(j)=='a'||x.charAt(j)=='A'){
count++;
}
else if(x.charAt(j)=='e'||x.charAt(j)=='E'){
count++;
}
else if(x.charAt(j)=='i'||x.charAt(j)=='I'){
count++;
}
else if(x.charAt(j)=='o'||x.charAt(j)=='O'){
count++;
}
else if(x.charAt(j)=='u'||x.charAt(j)=='U'){
count++;
}
else{
}
}
if(count>3){
System.out.println(x);
}
}
sc.close();
}
catch (Exception e) {
System.out.println("file not found");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.