Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.Collections;
- import java.util.*;
- public class SortReverse {
- public static void main(String[] args) {
- String[] strArgs = getArgs();
- System.out.println("Sorted:");
- sort(strArgs);
- System.out.println("Reversed:");
- reverse(strArgs);
- }
- public static String[] getArgs() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter words, one per line.");
- System.out.println("To quit, enter blank: ");
- String s = "";
- String word = "";
- do {
- word = scan.nextLine();
- s += word+" ";
- } while (!word.equals(""));
- String[] wordArray = s.split(" "); // create array of words
- return wordArray;
- }
- public static void sort (String[] args) {
- List<String> argList = Arrays.asList(args);
- Collections.sort( argList);
- for (String arg: argList) {
- System.out.format("%s ", arg);
- }
- System.out.println();
- }
- public static void reverse (String[] args) {
- List<String> argList = Arrays.asList(args);
- Collections.reverse( argList);
- for (String arg: argList) {
- System.out.format("%s ", arg);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement