-4

I have some of my code already done I just do not know how I would display the sentence in reverse order than what is entered. For example; if one enters "my name is joe" It should display in the output: Joe is name my

 import java.util.Scanner;


 public class Sentence {
     public static void main(String [] args) {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter a sentence: ");
         String sentence = input.nextLine();

         String[] words = sentence.split(" ");

         // Display the array of words in 
         // reverse order
     }
 }
john
  • 31
  • 5
  • 1
    You know how to use `System.out.print()`, do you know how to access an element in an array? If yes, then what is it you have problems with? I guess you are not the one who wrote this code, then. – RaminS Apr 21 '16 at 22:38
  • Thanks, only how to display it in reverse than what is entered. – john Apr 21 '16 at 22:39
  • 3
    What did you find after exhaustively investigating your use case in other Stack Overflow posts? – Savior Apr 21 '16 at 22:44
  • I only found questions relating to this in C++, not Java. – john Apr 21 '16 at 22:51

1 Answers1

2

If I understand your problem, you can do something like that:

public static void main(String [] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a sentence: ");
    String sentence = input.nextLine();
    String[] words = sentence.split(" ");
    for (int i = words.length - 1; i >= 0; i--) {
        System.out.println(words[i]);
    }
}
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22