r/javahelp • • Dec 31 '19

Java MOOC Exercise 59

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class WordsInReverseOrder {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    // create here an ArrayList
    ArrayList<String> words = new ArrayList<String>();
    System.out.println("Type a word:");
    String word = reader.nextLine();
    words.add(word);

    while (/*word != null &&*/!word.isEmpty()) {
        word = reader.nextLine();
        words.add(word);
    }
    if (word.isEmpty()) {
        System.out.println("You typed the following words: ");
        for (int i = words.size()-1; i >= 0; i--) {
            System.out.println(words.get(i));
        }
    }
}

}

Can anyone explain why I can't use the method reverse?

0 Upvotes

1 comment sorted by

0

u/[deleted] Dec 31 '19

You can access the array using Collections, which I see you’ve imported but not utilised.

You may want to re read the chapter on directly above this exercise, as it does state which methods you can access for this exact problem.

Happy NY, by the way!