(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
MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version
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)
- True or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forwardCan you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward
- (use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forward
- make a screen capture showing the StegExpose resultsarrow_forwardWhich of the following is not one of the recommended criteria for strategic objectives? Multiple Choice a) realistic b) appropriate c) sustainable d) measurablearrow_forwardManagement innovations such as total quality, benchmarking, and business process reengineering always lead to sustainable competitive advantage because everyone else is doing them. a) True b) Falsearrow_forward
- Vision statements are more specific than strategic objectives. a) True b) Falsearrow_forwardThe three components of the __________ approach to corporate accounting include financial, environmental, and social performance measures. Multiple Choice a) stakeholder b) triple dimension c) triple bottom line d) triple efficiencyarrow_forwardCompetitors, as internal stakeholders, should be included in the stakeholder management consideration of a company and in its mission statement. a) True b) Falsearrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning