Advertisement
dimipan80

Sort Array of Strings

Aug 17th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. /* Write a program that enters from the console number n and n strings,
  2.  * then sorts them alphabetically and prints them. */
  3.  
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class _08_SortArrayOfStrings {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.print("Enter a positive whole number of elements in your Sequence: ");
  13.         int count = scan.nextInt();
  14.  
  15.         if (count > 0) {
  16.             scan.nextLine();
  17.             String[] arrStr = new String[count];
  18.             for (int i = 0; i < arrStr.length; i++) {
  19.                 System.out.print("Enter the next word: ");
  20.                 arrStr[i] = scan.nextLine();
  21.             }
  22.  
  23.             Arrays.sort(arrStr);
  24.             System.out.println("The alphabetically sorted Sequence of these elements is:");
  25.             for (String word : arrStr) {
  26.                 System.out.println(word);
  27.             }
  28.         } else {
  29.             System.out.println("Error! - Invalid Input!!!");
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement