Question

In: Computer Science

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

● 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.

MY CODE IS INCORRECT PLEASE HELP

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");
       }

   }

}

Solutions

Expert Solution

// Please find the required solution

// add required imports for File handling
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

// add required imports for java util
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;


//
public class ArrayListAscending {

    public static void main(String[] args) {

        try {

            // create file instance
            File words = new File("words.txt");// please update the file path when required

            // provide file instance to scanner
            Scanner input = new Scanner(words);

            // define list
            List<String> list = new ArrayList<>();
            String line = "";

            //  add words upon iterating each line
            while (input.hasNextLine()) {
                line = input.nextLine();
                list.addAll(Arrays.asList(line.split(" ")));
            }

            // print list before sorting
            System.out.println("------- List Before Sorting ----------");
            System.out.println(list);

            // print list after sorting
            System.out.println("------- List After Sorting ----------");
            Collections.sort(list);
            System.out.println(list);

            // print list after sorting and starts with letter
            System.out.println("------- List After Sorting  and starts with letter ----------");
            for (String temp : list) {
                if (Character.isLetter(temp.charAt(0)))
                    System.out.println(temp);
            }

            //  finally close the scanner resource
            input.close();
        } catch (FileNotFoundException e) {
            // this exception is caught if the file is not found
            System.out.println("Error file not found");
        }

    }

}

Sample output:

code screenshot:


Related Solutions

How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write an assembly language program that reads move review information from a text file and reports...
Write an assembly language program that reads move review information from a text file and reports the overall scores for each movie as well as identifying the movie with the highest total score. There are four movie reviewers numbered from 1 to 4. They are submitting reviews for five movies, identified by the letters from “A” through “E”. Reviews are reported by using the letter identifying the movie, the review rating, which is a number from 0 to 100, and...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT