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

Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a program that reads from the external file input.txt, capitalizes all words that begin with...
Write a program that reads from the external file input.txt, capitalizes all words that begin with the letter "a," and then writes them to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Output: After the program generates output.txt, the code should display the contents of the file on the screen to verification.
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 program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT