In our lecture we learned about the StringTokenizer class. This class contains m
ID: 3547796 • Letter: I
Question
In our lecture we learned about the StringTokenizer class. This class contains methods which will do most of the heavy lifting when it comes to parsing strings. For this assignment, without using the StringTokenizer class we will parse an input file (from JFileChooser) and change every vowel (excluding y). We will also keep track of how many times each vowel appears in the file. (Convert each string of the file to lowercase).
Vowels: a e i o u
After a vowel is found, the next respective vowel will take its place.
For example:
I love aardvarks said Mary.
becomes:
o luvi eerdverks seod mery. (note the output should be lowercase)
for the case of 'u', replace it with 'a'.
Funky -> fanky
After you are done modifying each string, print it out on the console and then move on to the next line in the file.
When all strings have been printed, print out the number of times each vowel appeared in the file, and then a sum of all the vowels in the file.
Remember, strings in java are "immutable", meaning they cannot be shrunken down and letters cannot be changed inside of them directly.
Hint: Because strings can be considered simple arrays of characters, use the .toCharArray() method for each of the incoming lines and "build up" your string as you parse through the char[] array.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class PlayWithVowels {
public static void main(String[] args) {
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
int countY = 0;
char[] chars;
String s = null;
StringBuffer sb = new StringBuffer();
System.out.println("enter the file name");
Scanner scanner = new Scanner(System.in);
// The name of the file to read
String fileName = scanner.nextLine();
// read one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
chars = line.toCharArray();
for (int i=0; i< chars.length; i++){
s = Character.toString(chars[i]);
if (s.equals("a")){
s="e";
countA++;
sb.append(s);
continue;
}else if (s.equals("A")){
s="E";
countA++;
sb.append(s);
continue;
}else if (s.equals("e")){
s="i";
countE++;
sb.append(s);
continue;
}else if (s.equals("E")){
s="I";
countE++;
sb.append(s);
continue;
}else if (s.equals("i")){
s="o";
countI++;
sb.append(s);
continue;
}else if (s.equals("I")){
s="O";
countI++;
sb.append(s);
continue;
}else if (s.equals("o")){
s="u";
countO++;
sb.append(s);
continue;
}else if (s.equals("O")){
s="U";
countO++;
sb.append(s);
continue;
}else if (s.equals("u")){
s="y";
countU++;
sb.append(s);
continue;
}else if (s.equals("U")){
countU++;
s="Y";
sb.append(s);
continue;
}else if (s.equals("y")){
s="a";
countY++;
sb.append(s);
continue;
}else if (s.equals("Y")){
s="A";
countY++;
sb.append(s);
continue;
}
sb.append(s);
}
System.out.println("count of A : " + countA);
System.out.println("count of E : " + countE);
System.out.println("count of I : " + countI);
System.out.println("count of O : " + countO);
System.out.println("count of U : " + countU);
System.out.println("count of Y : " + countY);
}
System.out.println("changed string : " + sb);
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Please put the file in the root location " + fileName);
}
catch(IOException ex) {
System.out.println("Error reading file "+ fileName );
}
catch(NumberFormatException ex) {
System.out.println("All the entry in the file should be int values only" );
}
}
}
Hello,
Please find the code below for your problem....
Here I am reading from external file using scanner. Once u launch the application it will ask you for the complete file name...
For example, for me it is :: /Users/Desktop/test.txt
In my file I have the following text : - This is a test...
Now when i run my code, I will get the following output...
enter the file name
/Users/Desktop/test.txt
count of A : 2
count of E : 1
count of I : 2
count of O : 0
count of U : 0
count of Y : 0
changed string : Ehos os e tist
Code : -
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.