(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)
- 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-all-occurrences-of-a-substring-in-a-string/.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
- Q5. (Find the second lowest interger number) Write a program that prompts the user to enter a set of integer numbers, and finally displays the second lowest integer number in the set. To exit from the program enter -1. Here is a sample run Enter a set of integer numbers: 3 57 928-1 The second lowest number is 3.arrow_forward(IN THONNY/PYTHON CODE) design an application for the ABC Company that will process inventory from a file called ABC_Inventory.txt (attached to this assignment). The file contains Item ID, Description and list price stored on a separate line in the file. The program should display the contents of each record and then calculate and display the average list price. In python! ABC_INVENTORY BP25003-PERSON TENT75.0BP1000LOUNGE CHAIR50.0BP3000PROPANE HEATER149.0BP0900HIKING BOOTS120.0BP0950BACKPACK100.0BP3050GPS TRACKER130.0arrow_forwardWarning- Don't use AI or copied answer. I'll reduce rating if I see these things.arrow_forward
- Instructions(C++) Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges:0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200.Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189arrow_forwardQ2: Write a complete C++ program 5 poin 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(True/False): Win32 console input functions can detect when the user has resized the console window.arrow_forward
- -use loops and functions -Separate the implementation file from the definition file and header filearrow_forwardWrite code that uses any type of loop. The code should continually ask for a user input and sums all user inputs that are divisible by 2. The loop should continue until the user enters a negative number. The code should output the sum a single time once user entry has completed. You may assume all libraries and namespaces have been previously written into the code, you are just writing everything that would go inside the main function (beyond the return 0:).arrow_forward*python coding Driver’s License ExamThe local driver’s license office has asked you to create an application that grades the written portion of thedriver’s license exam. The exam has 20 multiple-choice questions. Hereare the correct answers:1. B 6. A 11. B 16. C2. D 7. B 12. C 17. C3. A 8. A 13. D 18. B4. A 9. C 14. A 19. D5. C 10. D 15. D 20. AYour program should store these correct answers in a list. The program should read thestudent’s answers for each of the 20 questions from a text file and store the answers inanother list. (Create your own text file to test the application.) After the student’s answershave been read from the file, the program should display a message indicating whether thestudent passed or failed the exam. (A student must correctly answer 15 of the 20 questionsto pass the exam.) It should then display the total number of correctly answered questions,the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly…arrow_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