Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hungdh;
- import java.util.Scanner;
- /**
- * @author hungdh
- *
- */
- public class CompareString {
- /*
- * Tạo 1 mảng các chuỗi là danh sách tên học sinh.
- * Nhập vào 1 chuỗi nào đó.
- * Sắp xếp danh sách tên theo thứ tự bảng chữ cái a, b, c.....
- * 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.
- *
- * 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):
- * Hoang Thi Yen, Nguyen Hoang Hiep, Vu Van Hoang, hoang, Hoang, hoang van tien, ...
- * (kết quả tìm kiếm không phân biệt chữ hoa và chữ thường!)
- */
- private String fullName[];
- private int num;
- private String temp;
- void Input() {
- Scanner inp = new Scanner(System.in);
- try {
- System.out.print("Numbers: ");
- num = inp.nextInt();
- } catch (Exception e) {
- System.out.println("Error: " + e.toString());
- }
- inp.nextLine();
- fullName = new String[num];
- for (int i = 0; i < num; i++) {
- System.out.print("Student " + (i + 1) + " fullName: ");
- fullName[i] = inp.nextLine();
- }
- }
- void Sort() {
- for (int i = 0; i < num - 1; i++)
- for (int j = (i + 1); j < num; j++) {
- // String name1 = fullName[i].split(" ")
- // Tách chuỗi fullname thành mảng các chuỗi dựa vào dấu space
- String[] tmp = fullName[i].split(" ");
- // Lấy tên
- String name1 = tmp[tmp.length - 1];
- tmp = fullName[j].split(" ");
- String name2 = tmp[tmp.length - 1];
- if (name1.compareTo(name2) > 0) {
- temp = fullName[i];
- fullName[i] = fullName[j];
- fullName[j] = temp;
- }
- }
- }
- void Show() {
- System.out.println("Student List");
- for (int i = 0; i < num; i++) {
- System.out.println(" " + fullName[i]);
- }
- }
- void Search(String find) {
- find = find.toLowerCase();
- boolean check = false;
- for (int i = 0; i < num; i++) {
- temp = fullName[i].toLowerCase();
- if (temp.indexOf(find) != -1) {
- System.out.println(fullName[i]);
- check = true;
- }
- }
- if (!check)
- System.out.println("Keyword not found.");
- }
- public static void main(String[] args) {
- CompareString unit25 = new CompareString();
- unit25.Input();
- unit25.Sort();
- unit25.Show();
- Scanner inp = new Scanner(System.in);
- System.out.print("Key word find: ");
- unit25.Search(inp.nextLine());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement