Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.Collections;
- public class Sorter
- {
- public static void main(String[] args)
- {
- @SuppressWarnings("resource")
- Scanner keyboard = new Scanner(System.in);
- ArrayList<String> wordList = new ArrayList<String>();
- int choice = 1;
- while(choice > 0)
- {
- System.out.println("What would you like to do?");
- System.out.println("1) Enter a new list of Words.");
- System.out.println("2) Display the last entered list of words in alphabetical order.");
- System.out.println("3) Show the current list of words.");
- System.out.println("0) End the program. ");
- System.out.print("> ");
- choice = Integer.parseInt(keyboard.nextLine());
- if(choice < 0 || choice > 3)
- {
- System.out.print("Invalid Choice!");
- continue;
- }
- else if(choice == 1)
- {
- getArrayList(wordList);
- }
- else if(choice == 2)
- {
- System.out.println("The word list in Alphabetical order is: " + listSort(wordList));
- }
- else if(choice == 3)
- {
- displayList(wordList);
- }
- }
- System.out.print("Goodbye!");
- }
- public static void getArrayList(ArrayList<String> wordList)
- {
- @SuppressWarnings("resource")
- Scanner input = new Scanner(System.in);
- System.out.println("Enter in a list of words. Enter the word 'Return' to return to the menu.");
- System.out.print("> ");
- String addToList = input.nextLine();
- String returner = "Return";
- while(!addToList.equalsIgnoreCase(returner))
- {
- System.out.print("> ");
- wordList.add(addToList);
- addToList = input.nextLine();
- }
- }
- public static void displayList(ArrayList<String> wordList)
- {
- for(String printer : wordList)
- {
- System.out.println(printer);
- }
- }
- public static String listSort(ArrayList<String> wordList)
- {
- String first = wordList.get(0);
- for(int i = 0; i < wordList.size(); i++)
- {
- for(int x = 0; x < wordList.size(); x++)
- {
- String after = wordList.get(x);
- if(first.compareToIgnoreCase(after) < 0)
- {
- after = first;
- }
- }
- }
- return wordList.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment