Write a Java 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 data 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 or List class.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class SortWords {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
BufferedReader br=null;
List<String>words=new ArrayList<>();
//handling with try catch for FileNotFound
try {
br = new BufferedReader(new FileReader(new File("input.txt")));
} catch (FileNotFoundException e) {
sc.close();
return;
}
//reading line by line
String line = br.readLine();
while (line != null) {
String arr[]=line.split(" ");
for(String word:arr) {
if(word.trim().length()!=0 && Character.isLetter(word.charAt(0))) {
words.add(word);
}
}
line=br.readLine();
}
sc.close();
br.close();
Collections.sort(words);
for(String s:words)
System.out.println(s);
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME
Get Answers For Free
Most questions answered within 1 hours.