In: Computer Science
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList.
THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT:
drunk
topography
microwave
accession
impressionist
cascade
payout
schooner
relationship
reprint
drunk
impressionist
schooner
THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED
THANK YOU IN ADVANCE
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
public class ArrayListAscending {
public static void main(String[] args) {
try {
File j = new
File("C:\\Users\\NaharaY\\Dropbox\\Jesse -23263\\Introduction to
Software Engineering\\Task 20\\words.txt");
Scanner input =
new Scanner(j);
List<String>list = new ArrayList<String>();
String line =
"";
while(input.hasNext()) {
list.addAll(Arrays.asList(line.split("
")));
}
Collections.sort(list);
for(String
temp:list) {
if(temp!=null &!temp.equals(""))
if(Character.isLetter(temp.charAt(0)))
System.out.println(temp);
}
input.close();
}catch(FileNotFoundException e)
{
System.out.println("Error");
}
}
}
Here is the completed code for this problem. Updated the code, so that the sorted words will be written to a file named output.txt in addition to printing to console. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// ArrayListAscending.java
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
public class ArrayListAscending {
public static void main(String[] args) {
try {
File j = new File(
"C:\\Users\\NaharaY\\Dropbox\\Jesse -23263\\Introduction to Software Engineering\\Task 20\\words.txt");
Scanner input = new Scanner(j);
List<String> list = new ArrayList<String>();
String line = "";
// creating a PrintWriter object to write the output to a file named
// output.txt
PrintWriter writer = new PrintWriter(new File("output.txt"));
while (input.hasNext()) {
// reading next line
line = input.nextLine();
// splitting line by spaces to create an array of words
String words[] = line.split(" ");
// looping through each word
for (String word : words) {
// adding to list only those words which start with a letter
if (word.length() > 0 && Character.isLetter(word.charAt(0))) {
list.add(word);
}
}
}
// sorting list
Collections.sort(list);
// looping through each word in list
for (String temp : list) {
// printing word
System.out.println(temp);
// writing word to output file
writer.println(temp);
}
input.close();
// closing output file, saving changes
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Error");
}
}
}