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

(Displaying Strings in Uppercase and Lowercase) Write an application that inputs a line of text and outputs the text twice- -once in all uppercase letters and once in all lowercase letters.

Short Answer

Expert verified
To display a string in both uppercase and lowercase, take user input, use string methods to change the case, and then output both versions.

Step by step solution

01

Input the line of text

Prompt the user to enter a line of text. Store the user's input in a variable.
02

Convert to uppercase

Use a string method to convert the entire line of text to uppercase and store it in a new variable.
03

Convert to lowercase

Use a string method to convert the entire line of text to lowercase and store it in another new variable.
04

Output the results

Print the uppercase version of the text followed by the lowercase version of the text to the user.

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.

Java Text Input
Handling text input in Java is a fundamental skill that enables programs to interact with users. To get started, Java provides several methods to read user input, but one of the most common and straightforward ways is to use the Scanner class from the java.util package.

To use the Scanner, you must first import it at the beginning of your code with the statement import java.util.Scanner;. After that, you can create a Scanner object to read from various input sources, including the keyboard. For reading a line of text from the user, you would typically use the nextLine() method of the Scanner object like this:
  • Create a Scanner object: Scanner scanner = new Scanner(System.in);
  • Prompt the user for input.
  • Read the input string: String userInput = scanner.nextLine();
Once you have the input stored in a variable, you can process it as needed by your program. Being familiar with handling text input is pivotal for creating interactive Java applications.
String to Uppercase
After obtaining the user input in Java, you may want to format or manipulate it. One common task is converting a String to uppercase. This can help normalize the text for searching, sorting, or simply for display purposes. In Java, this is easily accomplished using the toUpperCase() method that is available to all String objects.

To use this method, you invoke it on a String instance and it returns a new String where all original characters have been converted to their uppercase equivalent. The original String remains unchanged as Strings in Java are immutable. Here's a quick example:
  • String upperCaseText = userInput.toUpperCase();
Note that this method is locale-sensitive; it might produce unexpected results if the text contains locale-specific characters. By default, it uses the default locale, but you can also specify a Locale if needed.
String to Lowercase
Similarly, Java offers the ability to convert a String to lowercase using the toLowerCase() method. Converting a String to lowercase is commonly done when you want to make the text case-insensitive, for instance during comparisons or when storing user input for consistency.

Similar to the toUpperCase() method, toLowerCase() also returns a new String, leaving the original unaltered. Using the method looks like this:
  • String lowerCaseText = userInput.toLowerCase();
Again, this method considers the locale. For languages where case conversions depend on the context, this method might not produce accurate results in every situation, and you may need to use locale-specific methods. For most use cases, however, toLowerCase() works as expected and is a simple, yet powerful tool for String manipulation in Java.

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

(Random Sentences) Write an application that uses random-number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The application should generate and display 20 sentences. The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on".

(Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of text, tokenizes the line with String method sp 7 it and outputs the tokens in reverse order. Use space characters as delimiters.

For each of the following, write a single statement that performs the indicated task: a) Compare the string in \(s 1\) to the string in \(s 2\) for equality of contents. b) Append the string \(s 2\) to the string \(s 1, u \sin g+=\) c) Determine the length of the string in \(s 1\)

(Tokenizing and Comparing Strings) 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".

(Pig Latin) Write an application that encodes English-language phrases into pig Latin. Pig Latin is a form of coded language. There are many different ways to form pig Latin phrases. For simplicity, use the following algorithm: To form a pig Latin phrase from an English-language phrase, tokenize the phrase into words with String method split. To translate each English word into a pig Latin word, place the first letter of the English word at the end of the word and add the letters "ay." Thus, the word "jump" becomes "umpjay," the word "the" becomes "hetay," and the word "computer" becomes "omputercay." Blanks between words remain as blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Method printLatinWord should display each word. Each token is passed to method printLatinWord to print the pig Latin word. Enable the user to input the sentence. Keep a running display of all the converted sentences in a text area.

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