(Display words in ascending alphabetical order) Write a
Display words in ascending alphabetical order
Program plan:
- Import the required packages into the program.
- In the main() method,
- Check whether the argument length is not equal to 1. If yes,
- Display the error message.
- Assign the argument 0 as filename.
- Create a list to hold the words.
- In try block,
- Check the matches between the word from file and alphabets.
- Call the add() method to add the word into list.
- Catch the exception if error occurs.
- Call sort() method to sort the list in ascending order.
- Display the ascending order word.
- Check whether the argument length is not equal to 1. If yes,
The below program demonstrates to sort the word in ascending order from the file and display that list.
Explanation of Solution
Program:
//Import the java packages
import java.util.*;
import java.io.*;
//Class definition
public class Exercise20_01
{
//Main method
public static void main(String[] args)
{
/*Check whether the argument length is not equal to 1 */
if (args.length != 1) {
//Display the error message
System.out.println("Usage: java Exercise20_01 fullfilename");
System.exit(1);
}
//Assign the argument 0 as filename
String filename = args[0];
// Create a list to hold the words
List<String> list = new ArrayList<>();
//In try block
try {
//Create an object for Scanner class
Scanner in = new Scanner(new File(filename));
//Loop executes until the input from file
while (in.hasNext()) {
//Declare and assign the string //variable
String word = in.next();
//Check matches among the word from //file
if (word.matches("[a-z|A-Z].*"))
/*Call add() method to add the word into list */
list.add(word);
}
}
//Catch the exception
catch (Exception ex) {
//Display the error message
System.err.println(ex);
}
/*Call sort() method to sort the list in ascending order */
Collections.sort(list);
// Display the result
System.out.println("Display words in ascending order ");
//Loop executes until the list
for (String word : list) {
//Display the ascending order word
System.out.println(word);
}
}
}
Input file:
Screenshot of Test.txt file
Command to run the program:
java Exercise20_01 Test.txt
Display words in ascending order
hello
hi
hi
jhon
mercy
welcome
Want to see more full solutions like this?
Chapter 20 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Introduction To Programming Using Visual Basic (11th Edition)
Mechanics of Materials (10th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Degarmo's Materials And Processes In Manufacturing
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- Write code that does the following: opens a file named MyName.txt, reads the first linefrom the file and displays it, and then closes the file.arrow_forward-all-occurrences-of-a-substring-in-a-string/.arrow_forwardQ3: Write a C# program that reads two files (One.txt, Two.txt), where the first file contains the names of teachers and the other contains their addresses. Then, put the information (name and addresses) of the teachers to "Three.txt" file in the following form ">:>".arrow_forward
- Exercise #2 Frequency Analysis: One of the oldest approaches to breaking a code is to perform a frequency count of letters. Write a C++ program to perform a frequency count by reading the text from a file (code.txt). Your program should output how many A's are there in the text, how many B's are there, and so on. Note that the program will not make any distinction between uppercase and lowercase letters. It will output the results to the screen and in a text file (count.txt) Sample output (for code.txt): A: 3 B: 3 C: 23 D: 13 E: 41 F: 4 G: 9 H: 15 1:44 J: 1 K: 0 L: 7 M: 9 N: 33 O: 16 P: 4 Q:0 R: 22 S: 24 T: 27 U: 8 V: 5 W: 1 X: 0 Y: 4 Z: 0arrow_forwardSolve it in C language // use new method plsarrow_forwardIn C++ (don't copy paste the same answer that is already on this site, post unique code) Structures A menu-driven program gives the user the option to find statistics about different baseball players. The program reads from a file and stores the data for ten baseball players, including player’s team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online ) Write a program that declares a struct to store the data for a player. Declare an array of 10 components to store the data for 10 baseball players. The program prints out a menu (in a loop, so this can be done again and again) giving the user a choice to: print out all users and statistics print out the statistics for a specific player print out all data for a specific team update the data for a particular player (change one of the statistics) DO ALL WORK IN FUNCTIONS. USE A FUNCTION TO READ THE DATA, A FUNCTION TO PRINT THE MENU, and FUNCTIONS FOR EACH OF THE MENU…arrow_forward
- Python Problem Description: Kevin is a freelance video producer who makes TV commercials for local businesses. When he makes a commercial, he usually films several short videos. Later, he puts these short videos together to make the final commercial. He has asked you to write the following two programs. A program that allows him to enter the running time (in seconds) of each short video in a project. The running times are saved to a file. A successful test run for this program would be as shown below: A program that reads the contents of the file, displays the running times, and then displays the total running time of all the segments. A successful test run for this program would be as shown below:arrow_forward[JAVA Code] In this lab, you will open the file, flowers.dat, and read input from that file in a pre-written Java program. The program should read and print the names of flowers and whether they are grown in shade or sun. // Flowers.java - This program reads names of flowers and whether they are grown in shade or sun from an input // file and prints the information to the user's screen. // Input: flowers.dat. // Output: Names of flowers and the words sun or shade. import java.io.*; // Import class for file input. public class Flowers { public static void main(String args[]) throws Exception { // Declare variables here // Open input file. // Create BufferedReader object. // Write while loop that reads records from file. // Print flower name and the words sun or shade. br.close(); System.exit(0); } // End of main() method. } // End of Flowers class.arrow_forward(Count the letters in a string) Write a function that counts the number of letters in a string using the following header: def countLetters(s) : Write a test program that prompts the user to enter a string and displays the number of letters in the string. the answer should be in python.arrow_forward
- I need help with this homework question. Someone answered before but the output was incorrect. Programing Language: C++. Please do not use a string varible and add notes for understanding. 6.13: Word CountWrite a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a “word” to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file. Input Notes:There are two input sources in this program: the text file and standard input. The standard input consists of a single word, the name of the text file to be read. Output Notes (Prompts and Labels): The filename provided in standard input is prompted for with "Enter a file name: " and displayed so that the filename entered is on the same line as the prompt. The output of the program is a single line of the form: "The file contains N words." where N is the number of words determined by the…arrow_forwardSUBJECT NAME IS C++: WAP to read 4 student objects and write them into a file, then print total number of student number in file. After that read an integer and print details of student stored at this number in the file using random access and file manipulatorsarrow_forwarduse java languagearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education