Guest User

Untitled

a guest
Jun 4th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.Collections;
  4.  
  5. public class Sorter
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         @SuppressWarnings("resource")
  10.         Scanner keyboard = new Scanner(System.in);
  11.         ArrayList<String> wordList = new ArrayList<String>();
  12.        
  13.         int choice = 1;
  14.         while(choice > 0)
  15.         {
  16.             System.out.println("What would you like to do?");
  17.             System.out.println("1) Enter a new list of Words.");
  18.             System.out.println("2) Display the last entered list of words in alphabetical order.");
  19.             System.out.println("3) Show the current list of words.");
  20.             System.out.println("0) End the program. ");
  21.             System.out.print("> ");
  22.             choice = Integer.parseInt(keyboard.nextLine());
  23.             if(choice < 0 || choice > 3)
  24.             {
  25.                 System.out.print("Invalid Choice!");
  26.                 continue;
  27.             }
  28.             else if(choice == 1)
  29.             {
  30.                 getArrayList(wordList);
  31.             }
  32.             else if(choice == 2)
  33.             {
  34.                 System.out.println("The word list in Alphabetical order is: " + listSort(wordList));
  35.             }
  36.             else if(choice == 3)
  37.             {
  38.                 displayList(wordList);
  39.             }
  40.         }
  41.         System.out.print("Goodbye!");
  42.     }
  43.     public static void getArrayList(ArrayList<String> wordList)
  44.     {
  45.         @SuppressWarnings("resource")
  46.         Scanner input = new Scanner(System.in);
  47.         System.out.println("Enter in a list of words. Enter the word 'Return' to return to the menu.");
  48.         System.out.print("> ");
  49.         String addToList = input.nextLine();
  50.         String returner = "Return";
  51.         while(!addToList.equalsIgnoreCase(returner))
  52.         {
  53.             System.out.print("> ");
  54.             wordList.add(addToList);
  55.             addToList = input.nextLine();
  56.         }
  57.     }
  58.     public static void displayList(ArrayList<String> wordList)
  59.     {
  60.         for(String printer : wordList)
  61.         {
  62.             System.out.println(printer);
  63.         }
  64.     }
  65.     public static String listSort(ArrayList<String> wordList)
  66.     {
  67.         String first = wordList.get(0);
  68.         for(int i = 0; i < wordList.size(); i++)
  69.         {
  70.             for(int x = 0; x < wordList.size(); x++)
  71.             {
  72.                 String after = wordList.get(x);
  73.                 if(first.compareToIgnoreCase(after) < 0)
  74.                 {
  75.                     after = first;
  76.                 }
  77.             }
  78.         }
  79.     return wordList.toString();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment