Issue
Here's the code I have created using the collection and the scanner parameters. I've been so confused to combine it into recursive, I used iteration.
public class Main {
public static void main(String[] args) {
Deque deque = new LinkedList<>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your words (type R to reverse):");
while(sc.hasNext()) {
String line = sc.next();
if(line.toLowerCase().equals("r")) {
break;
}
deque.add(line);
}
System.out.println("\n===== Reversed Lines =====\n");
Iterator reverse = deque.descendingIterator();
while (reverse.hasNext()) {
System.out.println(reverse.next());
}
}
}
Solution
First of all, quick recap on recursion.
Every recursive method consists of two parts:
- Base case - that represents a simple edge-case (condition when recursion terminates) for which the outcome is known in advance.
- Recursive case - a part of a solution where recursive calls a made and when the main logic resides.
You can extract the code responsible for reading the user input into a separate method and substitute the while
loop with the recursive call.
The base case is represented by two situations: sc.hasNext()
yields false
and user input is equal to "r"
. Otherwise, the line will be added to queue and a new recursive call will be made.
And printing the content from the queue could be done recursively as well. The base case for this method is when reverse.hasNext()
returns false
. In the recursive case the next element will be printed, and a subsequent recursive call will be made.
So in order to be able to use Scanner
and Queue
you can either pass them as parameters or make them accessible globally.
Because of the requirements for this task, Scanner
and Queue
are declared locally inside the method readAndPrint()
and as parameters of the method readInput()
.
public class Main {
public void readAndPrint() {
Scanner sc = new Scanner(System.in);
Deque<String> deque = new LinkedList<>();
System.out.println("Enter your words (type R to reverse):");
readInput(sc, deque); // recursive helper-method that read from the console
System.out.println("\n===== Reversed Lines =====\n");
print(deque.descendingIterator()); // recursive helper-method that prints to the console
}
public static void print(Iterator<String> reverse) {
if (!reverse.hasNext()) { // base case
return;
}
System.out.println(reverse.next());
print(reverse); // recursive call
}
public void readInput(Scanner sc, Deque<String> deque) {
if (!sc.hasNext() && sc.hasNextLine()) { // no more input on this line, but there's at least one more line
sc.nextLine(); // advance to the next line
}
if (!sc.hasNext()) { // base case
return;
}
String line = sc.next();
if (line.equalsIgnoreCase("r")) { // base case
return;
}
deque.add(line);
readInput(sc, deque); // recursive call
}
public static void main(String[] args) {
ri.readAndPrint();
}
}
Output
Humpty Dumpty // multyline input
sat on a wall R
===== Reversed Lines =====
wall
a
on
sat
Dumpty
Humpty
Answered By - Alexander Ivanchenko
Answer Checked By - Senaida (JavaFixing Volunteer)