Advertisement
huyhung94

[KStudy] So sánh chuỗi

Nov 17th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package com.hungdh;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * @author hungdh
  7.  *
  8.  */
  9. public class CompareString {
  10.    
  11.     /*
  12.      * Tạo 1 mảng các chuỗi là danh sách tên học sinh.
  13.      * Nhập vào 1 chuỗi nào đó.
  14.      * Sắp xếp danh sách tên theo thứ tự bảng chữ cái a, b, c.....
  15.      * Tìm kiếm tất cả các tên học sinh giống tuyệt đối hoặc gần giống với từ khóa tìm kiếm đều được hiện ra.
  16.      *
  17.      * Ví dụ nhập từ khóa tìm kiếm: "hoang" thì sẽ hiện ra tất cả các tên sau (nếu như chúng có trong danh sách):
  18.      * Hoang Thi Yen, Nguyen Hoang Hiep, Vu Van Hoang, hoang, Hoang, hoang van tien, ...
  19.      * (kết quả tìm kiếm không phân biệt chữ hoa và chữ thường!)
  20.      */
  21.    
  22.    
  23.     private String fullName[];
  24.     private int num;
  25.     private String temp;
  26.  
  27.     void Input() {
  28.         Scanner inp = new Scanner(System.in);
  29.         try {
  30.             System.out.print("Numbers: ");
  31.             num = inp.nextInt();
  32.         } catch (Exception e) {
  33.             System.out.println("Error: " + e.toString());
  34.         }
  35.         inp.nextLine();
  36.         fullName = new String[num];
  37.         for (int i = 0; i < num; i++) {
  38.             System.out.print("Student " + (i + 1) + " fullName: ");
  39.             fullName[i] = inp.nextLine();
  40.         }
  41.     }
  42.  
  43.     void Sort() {
  44.         for (int i = 0; i < num - 1; i++)
  45.             for (int j = (i + 1); j < num; j++) {
  46.                 // String name1 = fullName[i].split(" ")
  47.                 // Tách chuỗi fullname thành mảng các chuỗi dựa vào dấu space
  48.                 String[] tmp = fullName[i].split(" ");
  49.                 // Lấy tên
  50.                 String name1 = tmp[tmp.length - 1];
  51.  
  52.                 tmp = fullName[j].split(" ");
  53.                 String name2 = tmp[tmp.length - 1];
  54.  
  55.                 if (name1.compareTo(name2) > 0) {
  56.                     temp = fullName[i];
  57.                     fullName[i] = fullName[j];
  58.                     fullName[j] = temp;
  59.                 }
  60.             }
  61.     }
  62.  
  63.     void Show() {
  64.         System.out.println("Student List");
  65.         for (int i = 0; i < num; i++) {
  66.             System.out.println("      " + fullName[i]);
  67.         }
  68.     }
  69.  
  70.     void Search(String find) {
  71.         find = find.toLowerCase();
  72.         boolean check = false;
  73.         for (int i = 0; i < num; i++) {
  74.             temp = fullName[i].toLowerCase();
  75.             if (temp.indexOf(find) != -1) {
  76.                 System.out.println(fullName[i]);
  77.                 check = true;
  78.             }
  79.         }
  80.         if (!check)
  81.             System.out.println("Keyword not found.");
  82.     }
  83.  
  84.     public static void main(String[] args) {
  85.         CompareString unit25 = new CompareString();
  86.         unit25.Input();
  87.         unit25.Sort();
  88.         unit25.Show();
  89.         Scanner inp = new Scanner(System.in);
  90.         System.out.print("Key word find: ");
  91.         unit25.Search(inp.nextLine());
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement