logo Practice-It logo

favoriteLetter

Language/Type: Java method basics input Scanner
Author: Marty Stepp (on 2010/12/28)

Write a method named favoriteLetter that accepts two parameters: a Scanner for the console, and a favorite letter represented as a one-letter String. The method repeatedly prompts the user until two consecutive words are entered that start with that letter. The method then prints a message showing the last word typed.

You may assume that the user will type a single-word response to each prompt. Your code should be case-sensitive; for example, if the favorite letter is a, you should not stop prompting if the user types words that start with an A. For example, the following logs represent the output from two calls to your method. (User input is underlined.)

Call:
Scanner console = new Scanner(System.in);
favoriteLetter(console, "y");
Scanner console = new Scanner(System.in);
favoriteLetter(console, "A");
Output:
Looking for two "y" words in a row.
Type a word: hi
Type a word: bye
Type a word: yes
Type a word: what?
Type a word: yellow
Type a word: yippee
"y" is for "yippee"
Looking for two "A" words in a row.
Type a word: I
Type a word: love
Type a word: CSE142!
Type a word: AND
Type a word: PROGRAMS
Type a word: are
Type a word: always
Type a word: Absolutely
Type a word: Awesome
"A" is for "Awesome"
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.