Reputation:
I am facing a problem taking all the lines from standard input and write them to standard output in reverse order. That is output each line in the reverse order of input.
Below is my code:
import java.util.Scanner;
public class ReverseOrderProgram {
public static void main(String args[]) {
//get input
Scanner sc = new Scanner(System.in);
System.out.println("Type some text with line breaks, end by
\"-1\":");
String append = "";
while (sc.hasNextLine()) {
String input = sc.nextLine();
if ("-1".equals(input)) {
break;
}
append += input + " ";
}
sc.close();
System.out.println("The current append: " + append);
String stringArray[] = append.split(" strings" + "");
System.out.println("\n\nThe reverse order is:\n");
for (int i = 0; i < stringArray.length; i++) {
System.out.println(stringArray[i]);
}
}
}
When I run my code with sample inputs like below:
Type some text with line breaks, end by "-1":
My name is John.
David is my best friend.
James also is my best friend.
-1
I get the following output:
The current append: My name is John. David is my best friend. James also is my best friend.
The reverse order is:
My name is John. David is my best friend. James also is my best friend.
Whereas, the required output is something like below:
The current append: My name is John. David is my best friend. James also is my best friend.
The reverse order is:
James also is my best friend.
David is my best friend.
My name is John.
Can anyone help me to check what is wrong with my code and fix it?
Upvotes: 1
Views: 2156
Reputation: 656
Try the below Code.
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class ReverseOrderProgram {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Type some text with line breaks, end by\"-1\":");
List<String> list= new LinkedList<String>();
String append = "";
while (sc.hasNextLine()) {
String input = sc.nextLine();
if ("-1".equals(input)) {
break;
}
list.add(input);
}
sc.close();
System.out.println("The current append: " + append);
Collections.reverse(list);
for (String string : list) {
System.out.println(string);
}
}
}
Hope This will help you
Upvotes: 3
Reputation: 4591
You can either use Collections.reverse()
as suggested by other answers. But the standard way of reversing is done using Stack
. Stack
is a LIFO data structure which exactly exhibits your required behaviour. You'll need to push all your results to a Stack
and pop
it until Stack
becomes empty
. Something like below snippet will give you an outline.
String input = " Hello \n World \n Here you go";
List<String> inputList = Arrays.asList(input.split("\n"));
Stack<String> myStringStack = new Stack<>();
myStringStack.addAll(inputList); // This is to exhibit your input scenario from user.
while (!myStringStack.isEmpty()) { // While your stack is not empty, keep popping!
System.out.println(myStringStack.pop());
}
Upvotes: 0
Reputation: 51
You can use a Stack data structure that has LIFO behavior for insert and read of elements. Tha more complete Java Stack implementation is Deque that has the method "descendingOrder" that returns an iterator of elements in reverse order.
Deque deque = new LinkedList();
// We can add elements to the queue in various ways
deque.add("Element 1 (Tail)"); // add to tail
deque.addFirst("Element 2 (Head)");
deque.addLast("Element 3 (Tail)");
deque.push("Element 4 (Head)"); //add to head
deque.offer("Element 5 (Tail)");
deque.offerFirst("Element 6 (Head)");
deque.offerLast("Element 7 (Tail)");
Iterator reverse = deque.descendingIterator();
System.out.println("Iterating over Deque with Reverse Iterator");
while (reverse.hasNext()) {
System.out.println("\t" + reverse.next());
}
Upvotes: 0
Reputation: 156
Edit - basically same implementation as previous answers, though uses a for loop:
import java.util.ArrayList;
import java.util.Scanner;
public class ReverseOrderProgram {
public static void main(String args[]) {
//create arraylist for lines
ArrayList<String> lines = new ArrayList<String>();
//get input
Scanner sc = new Scanner(System.in);
System.out.println("Type some text with line breaks, end by \"-1\":");
String append = "";
while (sc.hasNextLine()) {
String input = sc.nextLine();
if ("-1".equals(input)) {
break;
}
lines.add(input);
}
sc.close();
System.out.println("The current append: ");
for(String line : lines){
System.out.print(line + ". ");
}
System.out.println("\n\nThe reverse order is:\n");
for (int i = lines.size()-1; i >=0 ; i--) {
System.out.println(lines.get(i));
}
}
}
Upvotes: 0
Reputation: 2670
1 - 1 way to do it is run loop from backword.
for (int i = stringArray.length; i >=0 ; i++) {
System.out.println(stringArray[i]);
}
2 - Use Collections.reverse() method on list and print it. like
List<String> list = Arrays.asList(stringArray);
Collections.reverse(list );
System.out.println("Modified List: " + list );
Upvotes: 0
Reputation: 820
Instead of appending the input to the append string you should add the input string to a List and then print it from the bottom or use the Collections.reverse() method and then print it straight
Upvotes: 2