Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Write an application that reads a line of text, tokenizes it using space characters as delimiters and outputs only those words ending with the letters "ED".

Short Answer

Expert verified
Tokenize input text into words and filter by suffix 'ED'.

Step by step solution

01

Read the Input Text

Prompt the user to enter a line of text. Store this input for further processing. For example, you can use a variable named 'inputText' to hold the inputted string.
02

Split the Text into Tokens

Use a method to split the input text into a list of words, using space characters as delimiters. In many programming languages, this can be done using the 'split' function or method. For example: ```python words = inputText.split() ``` Here, 'words' will be a list of all the words in 'inputText', separated by spaces.
03

Filter Words Ending with 'ED'

Iterate over the list of words and check each word to see if it ends with 'ED'. You can use string functionalities that check the suffix of a word, such as 'endswith'. Add the words that satisfy this condition to a new list, say 'edWords'. For example: ```python edWords = [word for word in words if word.endswith('ED')] ```
04

Output the Filtered Words

Print or return the words that end with 'ED'. This will be the final output of your program. You can iterate over the 'edWords' list and print each word. For example: ```python for word in edWords: print(word) ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Tokenization
When working with text data in programming, especially in Java, one of the first steps you'll often perform is tokenization. Tokenization is the process of breaking down a piece of text into individual components, or tokens. These tokens are typically words, numbers, or symbols that are grouped based on specific delimiters. In the context of this exercise, the delimiter is a space character.

For example, if you have a sentence like "I love programming," tokenization will split this into three tokens: "I", "love", and "programming". In Java, this can be easily achieved using the `split()` method of the `String` class.
  • Use space as a delimiter with `split(" ")` to separate words.
  • The result is an array or list of tokens representing individual words from the input text.
Tokenization is an essential step when processing text data for further analyses, making tasks like filtering specific words approachable.
String Manipulation
String manipulation refers to techniques, methods, and operations that can be employed to modify or analyze strings in programming. This concept is crucial when working with text input in applications. In Java, string manipulation often involves using built-in methods to alter strings, compare them, or extract specific information.

In our exercise, once the text is tokenized into individual words, the next step involves filtering words that meet a particular condition: ending with "ED". To accomplish this, the `endsWith` method is particularly useful. This method checks whether a string ends with a specified suffix and returns a boolean value.
  • `endsWith("ED")` checks if a word concludes with the letters "ED".
  • It's essential in locating all the desired words from your list of tokens.
String manipulation makes it easy to perform complex operations on character sequences, significantly extending what you can achieve with text data.
List Comprehension
Although List Comprehension is not a native Java concept, it can be related to Java's way of processing collections efficiently. In languages like Python, list comprehension offers a concise way to create lists based on existing lists. This is accomplished by applying an expression to each element in the list and potentially filtering them.

In the context of this exercise, the code can be likened to list comprehension, where a new list of words ending with 'ED' is created based on a condition.
  • Iterate over each word in the tokenized list.
  • Apply a condition to selectively include words in the new list.
While Java requires more verbose syntax, using enhanced for loops or streams, the underlying logic is the same. Harnessing such patterns allows for powerful data manipulation with simpler and cleaner code structures.
Programming Exercise
Tackling programming exercises is a practical way to cement understanding of core concepts in Java, such as tokenization, string manipulation, and filtering. This specific exercise encourages you to apply multiple steps sequentially to solve a problem.

Steps include:
  • Prompting for user input to obtain a line of text.
  • Tokenizing that text, breaking it into words.
  • Manipulating strings to filter those of interest using conditions.
  • Outputting or printing the results.
Each part builds on the previous one, requiring an understanding of multiple programming concepts and their applications. As you continue to practice programming exercises like this one, you'll develop problem-solving skills that are critical for more advanced programming challenges.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

State whether each of the following is true or false. If false, explain why. a) When String objects are compared using ==, the result is true if the Strings contain the same values. b) A String can be modified after it is created

Write an application that inputs a line of text, tokenizes the line with an object of class StringTokenizer and outputs the tokens in reverse order. Use space characters as delimiters.

Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use an object of class StringTokenizer to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed. Remember that you will have to change delimiter characters during the tokenization process.

(Text Analysis) The availability of computers with string-manipulation capabilities has resulted in some rather interesting approaches to analyzing the writings of great authors. Much attention has been focused on whether William Shakespeare ever lived. Some scholars believe there is substantial evidence indicating that Christopher Marlowe actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writings of these two authors. This exercise examines three methods for analyzing texts with a computer. a) Write an application that reads a line of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase To be, or not to be: that is the question: contains one “a,” two “b’s,” no “c’s,” and so on. b) Write an application that reads a line of text and prints a table indicating the number of one-letter words, two-letter words, three-letter words, and so on, appearing in the text. For example, Fig. 30.25 shows the counts for the phrase Whether 'tis nobler in the mind to suffer c) Write an application that reads a line of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your application should include the words in the table in the same order in which they appear in the text. For example, the lines To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer contain the word “to” three times, the word “be” two times, the word “or” once, and so on. A more interesting (and useful) printout should then be attempted in which the words are sorted alphabetically.

Many popular word-processing software packages have built-in spell checkers. In this project, you are asked to develop your own spell-checker utility. We make suggestions to help get you started. You should then consider adding more capabilities. Use a computerized dictionary (if you have access to one) as a source of words. Why do we type so many words with incorrect spellings? In some cases, it is because we simply do not know the correct spelling, so we make a best guess. In some cases, it is because we transpose two letters (e.g., "defualt" instead of "default"). Sometimes we double-type a letter accidentally (e.g., "hanndy" instead of "handy"). Sometimes we type a nearby key instead of the one we intended (e.g., "biryhday" instead of "birthday"), and so on. Design and implement a spell-checker application in Java. Your application should maintain an array wordList of strings. Enable the user to enter these strings. [Note: In Chapter \(14,\) we have introduced file processing. With this capability, you can obtain the words for the spell checker from a computerized dictionary stored in a file.] Your application should ask a user to enter a word. The application should then look up that word in the wordList array. If the word is in the array, your application should print "Word is spelled correctly." If the word is not in the array, your application should print "Word is not spelled correctly." Then your application should try to locate other words in wordList that might be the word the user intended to type. For example, you can try all possible single transpositions of adjacent letters to discover that the word "default" is a direct match to a word in wordList. Of course, this implies that your application will check all other single transpositions, such as "edfault," "dfeault," "deafult," "defalut" and "defautl." When you find a new word that matches one in wordList, print it in a message, such as Did you mean "default"? Implement other tests, such as replacing each double letter with a single letter, and any other tests you can develop to improve the value of your spell checker.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free