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.

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

Here is the completed code for this problem. Updated the code, so that the sorted words will be written to a file named output.txt in addition to printing to console. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ArrayListAscending.java

import java.util.ArrayList;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

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 = "";

                  // creating a PrintWriter object to write the output to a file named

                  // output.txt

                  PrintWriter writer = new PrintWriter(new File("output.txt"));

                  while (input.hasNext()) {

                        // reading next line

                        line = input.nextLine();

                        // splitting line by spaces to create an array of words

                        String words[] = line.split(" ");

                        // looping through each word

                        for (String word : words) {

                              // adding to list only those words which start with a letter

                              if (word.length() > 0 && Character.isLetter(word.charAt(0))) {

                                    list.add(word);

                              }

                        }

                  }

                  // sorting list

                  Collections.sort(list);

                  // looping through each word in list

                  for (String temp : list) {

                        // printing word

                        System.out.println(temp);

                        // writing word to output file

                        writer.println(temp);

                  }

                  input.close();

                  // closing output file, saving changes

                  writer.close();

            } catch (FileNotFoundException e) {

                  System.out.println("Error");

            }

      }

}


Related Solutions

● 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...
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...
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT