Why Choose Us?
0% AI Guarantee
Human-written only.
24/7 Support
Anytime, anywhere.
Plagiarism Free
100% Original.
Expert Tutors
Masters & PhDs.
100% Confidential
Your privacy matters.
On-Time Delivery
Never miss a deadline.
You have been asked to read a text file word by word in an array of Strings, process the words, sort the words in alphabetical order, and save them in sorted order in another file with following specifications: 1
You have been asked to read a text file word by word in an array of Strings, process the words, sort the words in alphabetical order, and save them in sorted order in another file with following specifications:
1.Prompt user input for file names, input file name and output file name.
2.Count number of the words in the file, and then make an array of String with size equal to the number of the words in the file.
3.Reopen the file, read, and store each word of the file in the array.
4.Change all world to lower case.
5. Print number of the words, and frequency of each letter ('a' to 'z'), ignore cases, on the screen.
6.If words have any non-letter symbols in the beginning or at the end of the words, then remove them from both sides of the words. If still word contains any other non-letter symbol except hyphen '-', then get ride of the word, otherwise keep it.
7. Sort the words in alphabetic order. Note that you are not allowed to use Java build in methods to sort the words. You should develop your own mehtod for soting.
8. Save the words in a file in a unique order. If there are many occurrences of the same word in the array, then save only one instance of each word.
Check the sample unsorted.txt and sorted.txt files. Your output file may be a little different from sample output file provides here based on your processing methods. Check Sample.java program that reads file unsorted.txt and prints each word on the screen. Note that both Sample.java and unsorted.txt files should be in the same directory.
Expert Solution
Answer:
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ProcessStringFile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//these two arrays are for computing the letter frequencies
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int[] freq = new int[26];
//reader1 is for counting the number of words in the file
FileReader reader1;
//reader2 is for taking the words in the file and putting it in the array
FileReader reader2;
//taking the filenames for input and output
String inFile = "";
String outFile = "";
Scanner read;
//the number of elements in the array
//these changes depending on the number of words in the file
int index = 0;
do {
//we take input for our filenames
//if file is not found for reading then we take inputs again
try {
System.out.print("Enter file name for input: ");
inFile = input.nextLine();
reader1 = new FileReader(inFile + ".txt");
//We use scanner to take lines of the text file
read = new Scanner(reader1);
//while there is a new line we take whatever String is on the text file
break;
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
continue;
}
} while (true);
//we take input for the filename of our text ouput
System.out.print("Enter file name for output: ");
outFile = input.nextLine();
//now we we compute for the number of words
while (read.hasNextLine()) {
read.nextLine();
index++;
}
//the number of words now corresponds to the number of elements
//in the array
String[] words = new String[index];
//we now again read the file, insert each word in the array
//and then process it according to the requirements
try {
reader2 = new FileReader(inFile + ".txt");
//We use scanner to take lines of the text file
read = new Scanner(reader2);
//while there is a new line we take whatever String is on the text file
int count = 0;
while (read.hasNextLine()) {
//we take Strings on the line. we automatically convert the word
//to lowercase
words[count] = read.nextLine().toLowerCase();
//we check if the beginning and end is not within a to z
//if not then we remove those characters
if (words[count].charAt(0) < 'a'
|| words[count].charAt(0) > 'z') {
words[count] = words[count].substring(1, words[count].length() - 1);
}
if (words[count].charAt(words[count].length() - 1) < 'a'
|| words[count].charAt(words[count].length() - 1) > 'z') {
words[count] = words[count].substring(0, words[count].length() - 2);
}
//if there is still a non-letter character
//here we remove it except for -
for (int i = 0; i < words[count].length(); i++) {
if (words[count].charAt(i) < 'a'
|| words[count].charAt(i) > 'z') {
if (words[count].charAt(i) != '-') {
words[count] = "";
break;
}
}
}
//we count the number of frequency per letter here
//the number of frequencies depends on the previous words
//not on the current words that are removed
for (int i = 0; i < words[count].length(); i++) {
for (int j = 0; j < letters.length; j++) {
if (words[count].charAt(i) == letters[j]) {
freq[j]++;
}
}
}
//we also remove duplicate words here
for (int i = 0; i < count; i++) {
if (words[count].equals(words[i])) {
words[count] = "";
}
}
count++;
}
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
//we close the file reader
read.close();
//we sort our words here
for (int i = 0; i < index; i++) {
for (int j = i + 1; j < index; j++) {
if (words[i].compareTo(words[j]) > 0) {
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
//Displaying the strings after sorting
System.out.println("Number of words: " + index);
System.out.println("Frenquery of letters: ");
for (int i = 0; i < freq.length; i++) {
System.out.println(letters[i] + ": " + freq[i]);
}
try {
FileWriter writer = new FileWriter(outFile + ".txt", false);
System.out.println("Strings that were processed and sorted:");
for (int i = 0; i < index; i++) {
if (!words[i].equals("")) {
System.out.println(words[i]);
writer.write(words[i] + "\r\n");
}
}
writer.close();
} catch (IOException ex) {
System.out.println("IO Exception");
}
}
}
run: Enter file name for input: hey File not found. Enter file name for input: input Enter file name for output: output Number of words: 11 Frenquery of letters: a: 7 b: 0 c: 0 d: 3 e: 8 f: 0 g: 3 h: 1 i: 1 j: 0 k: 3 l: 0 m: 1 n: 6 o: 2 p: 0 q: 0 r: 0 s: 5 t: 1 u: 0 v: 0 w: 0 x: 0 y: 5 z: 0 Strings that were processed and sorted: agenda en game hidden snakey stag-es yo BUILD SUCCESSFUL (total time: 7 seconds)
sample input.txt
sk&ter 1yo$ Stag-eS GAmE hiddEn agenda Snakey SNakey End8 SNakey yo
sample output.txt
agenda en game hidden snakey stag-es yo
Archived Solution
You have full access to this solution. To save a copy with all formatting and attachments, use the button below.
For ready-to-submit work, please order a fresh solution below.





