In: Computer Science
in JAVA, Project: Displaying Words in Ascending Alphabetical Order
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.
1. You must use one of following Concrete class to store date from
the input.txt file.
Vector, Stack, ArrayList, LinkedList
2. To sort those words, you should use one of existing interface methods available in Collection class.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class WordSort { public static void main(String[] args) { String fileName = "nput.txt"; ArrayList<String> wordList = new ArrayList<>(); try { Scanner fileReader = new Scanner(new File(fileName)); while (fileReader.hasNext()) { String word = fileReader.next().toLowerCase(); if (word.length() > 0 && Character.isAlphabetic(word.charAt(0))) wordList.add(word); } fileReader.close(); } catch (FileNotFoundException e) { System.out.println("Error: Could not find file: " + fileName); return; } // Reading of all words from file done till here //2. To sort those words, you should use one of existing // interface methods available in Collection class. Collections.sort(wordList); System.out.println("Here are the sorted words in alphabetical order:"); for (String word : wordList) System.out.println(word); } }
======================================================================