Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 20, Problem 20.1PE

(Display words in ascending alphabetical order) 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.

Expert Solution & Answer
Check Mark
Program Plan Intro

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.
Program Description Answer

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);

}

}

}

Sample Output

Input file:

Screenshot of Test.txt file

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition), Chapter 20, Problem 20.1PE

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
(Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows: A password must have at least eight characters. A password consists of only letters and digits. A password must contain at least two digits. Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise.
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…
- The whole program should include one loop and one If statement.

Chapter 20 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 20.4 - Prob. 20.4.2CPCh. 20.5 - Prob. 20.5.1CPCh. 20.5 - Suppose list1 is a list that contains the strings...Ch. 20.5 - Prob. 20.5.3CPCh. 20.5 - Prob. 20.5.4CPCh. 20.5 - Prob. 20.5.5CPCh. 20.6 - Prob. 20.6.1CPCh. 20.6 - Prob. 20.6.2CPCh. 20.6 - Write a lambda expression to create a comparator...Ch. 20.6 - Prob. 20.6.4CPCh. 20.6 - Write a statement that sorts an array of Point2D...Ch. 20.6 - Write a statement that sorts an ArrayList of...Ch. 20.6 - Write a statement that sorts a two-dimensional...Ch. 20.6 - Write a statement that sorts a two-dimensional...Ch. 20.7 - Are all the methods in the Collections class...Ch. 20.7 - Prob. 20.7.2CPCh. 20.7 - Show the output of the following code: import...Ch. 20.7 - Prob. 20.7.4CPCh. 20.7 - Prob. 20.7.5CPCh. 20.7 - Prob. 20.7.6CPCh. 20.8 - Prob. 20.8.1CPCh. 20.8 - Prob. 20.8.2CPCh. 20.8 - Prob. 20.8.3CPCh. 20.9 - How do you create an instance of Vector? How do...Ch. 20.9 - How do you create an instance of Stack? How do you...Ch. 20.9 - Prob. 20.9.3CPCh. 20.10 - Prob. 20.10.1CPCh. 20.10 - Prob. 20.10.2CPCh. 20.10 - Prob. 20.10.3CPCh. 20.11 - Can the EvaluateExpression program evaluate the...Ch. 20.11 - Prob. 20.11.2CPCh. 20.11 - If you enter an expression "4 + 5 5 5", the...Ch. 20 - (Display words in ascending alphabetical order)...Ch. 20 - (Store numbers in a linked list) Write a program...Ch. 20 - (Guessing the capitals) Rewrite Programming...Ch. 20 - (Sort points in a plane) Write a program that...Ch. 20 - (Combine colliding bouncing balls) The example in...Ch. 20 - (Game: lottery) Revise Programming Exercise 3.15...Ch. 20 - Prob. 20.9PECh. 20 - Prob. 20.10PECh. 20 - (Match grouping symbols) A Java program contains...Ch. 20 - Prob. 20.12PECh. 20 - Prob. 20.14PECh. 20 - Prob. 20.16PECh. 20 - (Directory size) Listing 18.10,...Ch. 20 - Prob. 20.20PECh. 20 - (Nonrecursive Tower of Hanoi) Implement the...Ch. 20 - Evaluate expression Modify Listing 20.12,...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
the method rollDice.

Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)

True or False: The do-while loop is a pretest loop.

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
C++ Programming Tutorial 36 - Intro to Loops; Author: Caleb Curry;https://www.youtube.com/watch?v=M3o7Y0juEP0;License: Standard YouTube License, CC-BY