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 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...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT