/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 10 (Displaying Strings in Uppercase... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

(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 91Ó°ÊÓ!

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

(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.

( Tokenizing and Comparing Strings) Write an application that reads a line of text, tokenizes the line using space characters as delimiters and outputs only those words beginning with the letter "b".

(Project: \(A\) Spelling Checker) Many popular word-processing software packages have builtin spell checkers. In this project, you're 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's because we simply do not know the correct spelling, so we make a best guess. In some cases, it's 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 17, we introduce 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.

(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".

(Comparing Strings) Write an application that uses String method compareTo to compare two strings input by the user. Output whether the first string is less than, equal to or greater than the second.

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.