Question

Write a Java program that reads words from a text file and displays all the words...

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.

Homework Answers

Answer #1
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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that reads words form a text file and displays all the words (duplicates...
Write a program that reads words form a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... Salami apple banana banana boat zebra
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
JAVA: Derive following statistics from some text file: number of words that are size 6 or...
JAVA: Derive following statistics from some text file: number of words that are size 6 or more, number of words used only once, number of words used such that each word is only counted once!! Use ArrayList and HashSet.
Java File I/O Write a brief program that writes your name to a file in text...
Java File I/O Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
java program; Write a brief program that writes your name to a file in text format...
java program; Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Write a brief program in JAVA that writes your name to a file in text format...
Write a brief program in JAVA that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
Write a Java program that reads employee information from the file “employee.txt” then the program will...
Write a Java program that reads employee information from the file “employee.txt” then the program will display his/her information on the screen. The file contains the Employee’s Name, age,join date, quit date, and salary. • For each year the employee is in the company,10 days of sick leave are allowed. So, if an employee is in the company for two years, 20 days of sick leave are allowed. • For each year the employee is in the company, one-month salary...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT