(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
Web Development and Design Foundations with HTML5 (8th Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
Starting Out with C++: Early Objects
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Computer Science: An Overview (12th Edition)
- C#- Given a text file, refer to the example and write a program (console application) that counts the number of words that end with the letter "t" or "e" (not case sensitive). We’ll say that a "t" or "e" is at the end of a word if there is not an alphabetic letter immediately following it. The program should output the resulting count.arrow_forward(PYTHON) A photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a program that reads the name of a text file containing a list of photo file names. The program then reads the photo file names from the text file, replaces the "_photo.jpg" portion of the file names with "_info.txt", and outputs the modified file names. Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output. Ex: If the input of the program is: ParkPhotos.txt and the contents of ParkPhotos.txt are: Acadia2003_photo.jpg AmericanSamoa1989_photo.jpg BlackCanyonoftheGunnison1983_photo.jpg CarlsbadCaverns2010_photo.jpg CraterLake1996_photo.jpg GrandCanyon1996_photo.jpg IndianaDunes1987_photo.jpg LakeClark2009_photo.jpg Redwood1980_photo.jpg…arrow_forwardThis is the file :arrow_forward
- ENGAGE MINDTAP -Using a Counter-Controlled while Loop in Java Using a Counter-Controlled while Loop ? Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions 1. Ensure the file named Multiply.java is open. 2. Write a counter-controlled while loop that uses the loop control variable to take on the values O through 10. Remember to initialize the loop control variable before the program enters the loop. 3. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Remember to change the value of the loop control variable in the body of the loop. 4. Execute the program by clicking Run. Record the output of this program. Grading When you have completed your program, click the Submit button to record your…arrow_forwardWhen you open a file for reading, what happens if the file does not exist? When you open a file for writing, what happens if the file already exists?arrow_forwardWhat happens if the file already exists when you open a file for output?arrow_forward
- Q2: Write a complete C++ program that reads the contents of the text file "mydata.txt", word by word, and appends them to the end of text file "myoutput.txt" . The main function calculates and prints the number of characters read and copied between the files.arrow_forward(Use DevC++) 2. Student GradeWrite a program that reads a student name followed by five test scores. The program should output the student name, the five test scores, and the average test score. Output the average test score withtwo decimal places. The data to be read is stored in a file called test.txt. The output should be stored in a file calledtestavg.out. Input: A file containing the student name and the five test scores. A sample input is:Andrew Miller 87.50 89 65.75 37 98.50 Output: The student name, the five test scores, and the average of the five test scores, saved to a file. The program needs to read a student’s first and last name and five test scores. Therefore, you need two variables to store the student name and five variables to store the five test scores. To find theaverage, you must add the five test scores and then divide the sum by 5. Thus, you need a variable to store the average test score. Furthermore, because the input data is in a file, you need an…arrow_forwardWrite 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
- Q2: Write a c++ program that reads a text file that has a poem written in it. (Write down the poem yourself in a text file manually). Your program should display that poem and count a total number of lines in the file.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_forwardExercise #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_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