Question

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. The text file is passed as a command-line argument. Here is a sample run:Words in ascending order: Java String System This Welcome Welcome args class main message out println prints program public public static to void welcomeI have provided an outl.txt file for you to test with This contains a word that doesn t start with a letter, so be sure to handle that.

out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with

https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself

This program prints a welcome message public class Welcome public static void main String args ys System out println Welcome to Java //notaword eut printla aoe

Please include comments as I am in this for learning and would really appreciate the help!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class AscendingOrder {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "out1.txt";

        Scanner reader = new Scanner(new File(fileName));

        ArrayList<String> words = new ArrayList<>();

        // while the file has words, keep reading them one by one
        while(reader.hasNext()) {
            String word = reader.next();

            // check if word start with a letter
            if(Character.isAlphabetic(word.charAt(0))) {
                words.add(word);
            }
        }

        // sort the words found
        Collections.sort(words);

        // print the list now
        System.out.println("Words in ascending order:");
        for(String w: words) {
            System.out.println(w);
        }

        reader.close();

    }

}

Console × i/X2 k h E4G](E-旦▼ en Test java b) Sortings.java D Sorting.java D BinaryTree.java D Ascend ingOrderjava× ▼ ヨoutl.bt <terminated> AscendingOrder [Java Application] CAProgram Files\Ja 1import java.io.File Words in ascending order: Java String System This Welcome Welcome 2 import java.io.FileNotFoundException; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Scanner 6 7 public class AscendingOrder 9 public static void main(String[] args) throws FileNotFoundException ( String fileNameout1.txt; Scanner reader- new Scanner (new File(fileName)); ArrayList<string words new ArrayList<>); // while the file has words, keep reading them one by one 10 args class main message ou println prints program public public static to void welcome 12 13 14 15 16 while(reader.hasNext)) 18 String word reader.next); 20 21 // check if word start with a letter İf(Character.isAlphabetic(word.charAt(0))) { words.add(word); 23 25 26 27 28 // sort the words found Collections.sort(words); // print the list now System.out.println(Words in ascending order:); for (String w: words) f 30 32 System.out.println(w) reader.close() 35 36 38 39 40

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

  • 9. A concordance is an alphabetical word list for a passage of text. Each word in...

    9. A concordance is an alphabetical word list for a passage of text. Each word in the concordance is mapped to an integer indicating the frequency of the word's occurrence. The constructor of Concordance has one String parameter that identifies the text file to be read. An incompleteConcordance class is below. public class Concordance public Concordance (String nameOfFileH concord new TreeMap<String, Integer 0 createConcordance (nameOfFile); //Constructor //nameOfFile the text file being read public void createConcordance (string filename)...) //Create a TreeMap...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat...

    Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat and prints out the sum of the positive values from the file. Assume that the file contains only numeric values. You must output an error if the file can't be opened. You must write the complete program and the output when the program runs successfully must conform exactly to the sample output. Bonus ( 5 pts). Output an error if the file contains non-numeric...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • 1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...

    1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...

  • Lab Description Sort all words by comparing the length of each word. The word with the...

    Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have more than one word with the same length, that group would be sorted alphabetically Input: The data file contains a list of words. The first line in the data file is an integer that represents the number of data sets to follow Output: Output the complete list of words in order by length. Sample Data 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT