Advertisement
f0rkB0mb

Dung Harrys CodeSnippet

Sep 29th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AlphabetSorting {
  4.     private String name[] = new String[100];
  5.     private int numberName = 0;
  6.  
  7.     public AlphabetSorting() {
  8.         this.initialize();
  9.     }
  10.  
  11.     private void initialize() {
  12.         for(int i = 0; i < this.name.length; i ++) {
  13.             this.name[i] = new String("");
  14.         }
  15.     }
  16.  
  17.     public void enterNames() {
  18.         Scanner scanner = new Scanner(System.in);
  19.         String temp = new String("");
  20.         boolean flag = true;
  21.  
  22.         do {
  23.             System.out.print("Enter number of the name you wanted to sort: ");
  24.             temp = scanner.nextLine();
  25.  
  26.             for(int i = 0; i < temp.length(); i ++) {
  27.                 if((temp.charAt(i) <= '0') && (temp.charAt(i) >= '9')) {
  28.                     flag = false;
  29.                 }
  30.             }
  31.  
  32.             if(!flag) {
  33.                 System.out.println("**Error: Your entering is invalid. Enter again");
  34.             } else {
  35.                 this.numberName = Integer.parseInt(temp);
  36.                 if(this.numberName > 100) {
  37.                     System.out.println("**Error: Your number of names is greater than 100");
  38.                 }
  39.             }
  40.         } while((!flag) && (this.numberName > 100));
  41.  
  42.         System.out.println("Begin entering each name...");
  43.  
  44.         for(int i = 0; i < numberName; i ++) {
  45.             System.out.print("Enter name " + (i + 1) + ": ");
  46.             this.name[i] = scanner.nextLine();
  47.         }
  48.  
  49.         System.out.println("Finish entering name part...");
  50.     }
  51.  
  52.     private void swapNames(int firstIndex, int secondIndex) {
  53.         String temp = new String("");
  54.  
  55.         temp = this.name[firstIndex];
  56.         this.name[firstIndex] = this.name[secondIndex];
  57.         this.name[secondIndex] = temp;
  58.     }
  59.  
  60.     private int charAt(int nameIndex, int index) {
  61.         if((index >= this.name[nameIndex].length()) || (nameIndex >= this.numberName)) {
  62.             return 1;
  63.         } else {
  64.             return this.name[nameIndex].charAt(index);
  65.         }
  66.     }
  67.  
  68.     private void sortRange(int index, int head, int tail) {
  69.         for(int i = head; i <= tail; i ++) {
  70.             for(int j = i; j <= tail; j ++) {
  71.                 if(this.charAt(i, index) > this.charAt(j, index)) {
  72.                     int firstSum = 0, secondSum = 0;
  73.                     for(int m = 0; m < index; m ++) {
  74.                         firstSum += this.charAt(i, m);
  75.                         secondSum += this.charAt(j, m);
  76.                     }
  77.                     if(firstSum == secondSum) {
  78.                         this.swapNames(i, j);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     private int getMaxLengthName() {
  86.         int tmp = this.name[0].length();
  87.  
  88.         for(int i = 0; i < this.numberName; i ++) {
  89.             if(this.name[i].length() > tmp) {
  90.                 tmp = this.name[i].length();
  91.             }
  92.         }
  93.  
  94.         return tmp;
  95.     }
  96.  
  97.     public void sortAlphabet() {
  98.         int maxLength = this.getMaxLengthName();
  99.         int head, tail;
  100.         char temp;
  101.  
  102.         this.sortRange(0, 0, this.numberName - 1);
  103.  
  104.         for(int j = 1; j < maxLength; j ++) {
  105.             int i = 0;
  106.  
  107.             while(i < this.numberName) {
  108.                 head = i;
  109.  
  110.                 temp = (char) this.charAt(i, j - 1);
  111.  
  112.                 while(this.charAt(++ i, j - 1) == temp) {
  113.                     if(i >= this.numberName - 1) {
  114.                         i ++;
  115.                         break;
  116.                     }
  117.                 }
  118.  
  119.                 tail = i - 1;
  120.  
  121.                 this.sortRange(j, head, tail);
  122.             }
  123.         }
  124.     }
  125.  
  126.     public void printNames() {
  127.         for(int i = 0; i < this.numberName; i ++) {
  128.             System.out.println("Name " + (i + 1) + ": " + this.name[i]);
  129.         }
  130.     }
  131.  
  132.     public static void main(String[] args) {
  133.         AlphabetSorting sorter = new AlphabetSorting();
  134.         sorter.enterNames();
  135.  
  136.         sorter.sortAlphabet();
  137.  
  138.         System.out.println("The result after sorting is: \n");
  139.  
  140.         sorter.printNames();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement