Problem

Many times, you have used Scanner methods to read and parse keyboard input. You can also u...

Many times, you have used

Scanner
methods to read and parse keyboard input. You can also use those same
Scanner
methods to read and parse an ordinary
String.
The text’s
printReverseMessage
method in Figure 11.7 used
String’s charAt
method to reverse the order of the letters in a string. This exercise asks you to use
Scanner’s next
method to reverse the order of the words in a string. The program below should reverse the words in the string, “We Are Many,” and return the string, “Many Are We.” In the recursive method in this program, supply the code where it says <code-fragment>.

import java.util.Scanner;public class ReverseWords{  public static void main (String[] args)  {      String message = new String ("We Are Many");      Scanner scan = new Scanner (message);      String reversedMessage = getReverse (scan);      System.out.println (reversedMessage);  } // end main  //**************************************************  public static String getReverse (Scanner scan)  {      String nextWord;      <code-fragment>  } // end getReverse} // end class ReverseWords

Do not use an array. Use

Scanner’s next
method to step through the message in the
scan
object, retrieve each
nextWord,
and
return
the return from a recursive call plus that
nextWord.
What changes with each recursive call? What is the stopping condition? What does the
getReverse
method return when the stopping condition is satisfied?

Figure Recursive implementation of the printReverseMessage method

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 11
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT