I\'m hoping someone can write this program for me: In our lecture we learned abo
ID: 3548058 • Letter: I
Question
I'm hoping someone can write this program for me:
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.*;
import java.util.*;
import javax.swing.*;
public class Project1{
static File file;
static JFrame frame = new JFrame("Input a file to analyze: ");
public static void main(String arg[]) throws Exception
{
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
frame.setVisible(false);
int retVal = fc.showOpenDialog(frame);
if (retVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
frame.setVisible(false);
}
String content = new Scanner(file).useDelimiter("\Z").next().toLowerCase();
char[] charArray = content.toCharArray();
int index=0;
int[] countArray = new int[5];
while(index<charArray.length)
{
switch(charArray[index]){
case 'a':
charArray[index] ='e';
countArray[0]++;
break;
case 'e':
charArray[index] ='i';
countArray[1]++;
break;
case 'i':
charArray[index] ='o';
countArray[2]++;
break;
case 'o':
charArray[index] ='u';
countArray[3]++;
break;
case 'u':
charArray[index] ='a';
countArray[4]++;
break;
}
index++;
}
System.out.println(new String(charArray));
System.out.println("Number of occurences of 'a': "+ countArray[0]);
System.out.println("Number of occurences of 'e': "+ countArray[1]);
System.out.println("Number of occurences of 'i': "+ countArray[2]);
System.out.println("Number of occurences of 'o': "+ countArray[3]);
System.out.println("Number of occurences of 'u': "+ countArray[4]);
System.out.println("Total count of vowels: "+ (countArray[0]+countArray[1]+countArray[2]+countArray[3]+countArray[4]));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.