Skip to content

Instantly share code, notes, and snippets.

@witoong623
Last active August 29, 2015 14:15
Show Gist options
  • Save witoong623/fdb73ea36723c2fe9db7 to your computer and use it in GitHub Desktop.
Save witoong623/fdb73ea36723c2fe9db7 to your computer and use it in GitHub Desktop.
public class Student {
private String Search;
public void setSearch(String keyword) {
keyword = Search;
}
public String getSearch() {
return Search;
}
}
import java.util.*;
import java.io.IOException;
public class StudentInformation {
public static void main(String[] args) throws IOException {
int choice;
StudentSearch student = new StudentSearch();
Scanner scan = new Scanner(System.in);
System.out.print("Select menu to search (1 = ID, 2 = Front name, 3 = Name, 4 = LastName) : ");
try {
choice = scan.nextInt();
if (choice == 1) {
System.out.print("Enter ID : ");
if (student.SearchByID(scan.next())) {
student.show();
}
else {
System.out.println("Data not found.");
}
}
else if (choice == 2) {
System.out.print("Enter Frontname : ");
if (student.SearchByFrontName(scan.next())) {
student.show();
}
else {
System.out.println("Data not found.");
}
}
else if (choice == 3) {
System.out.print("Enter Name : ");
if (student.SearchByName(scan.next())) {
student.show();
}
else {
System.out.println("Data not found.");
}
}
else if (choice == 4) {
System.out.print("Enter Lastname : ");
if (student.SearchByID(scan.next())) {
student.show();
}
else {
System.out.println("Data not found.");
}
}
else {
System.out.println("Choice wrong.");
}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
finally {
scan.close();
}
}
}
import java.io.*;
import java.util.*;
public class StudentSearch extends Student {
private String ID;
private String FrontName;
private String Name;
private String Lastname;
private String[][] StudentInfo;
private String[] StudentInfoByFront;
public StudentSearch() throws IOException {
int lineNumber = 0;
Scanner findLineNumber = new Scanner(new File("D:/Student List.txt"));
while (findLineNumber.hasNextLine()) {
findLineNumber.nextLine();
lineNumber++;
}
findLineNumber.close();
StudentInfo = new String[lineNumber][4];
Scanner in = new Scanner(new File("D:/Student List.txt"));
for (int i = 0; i < lineNumber; i++) {
StudentInfo[i][0] = in.next();
StudentInfo[i][1] = in.next();
StudentInfo[i][2] = in.next();
StudentInfo[i][3] = in.next();
}
in.close();
}
public boolean SearchByID(String id) {
for (int i = 0; i < StudentInfo.length; i++) {
if (id.equalsIgnoreCase(StudentInfo[i][0])) {
ID = StudentInfo[i][0];
FrontName = StudentInfo[i][1];
Name = StudentInfo[i][2];
Lastname = StudentInfo[i][3];
return true;
}
}
return false;
}
public boolean SearchByFrontName(String front) {
if (!front.equalsIgnoreCase("miss") && !front.equals("mr.")) {
return false;
}
int lineNumber = 0;
for (int i = 0; i < StudentInfo.length; i++) {
if (front.equalsIgnoreCase(StudentInfo[i][1])) {
lineNumber++;
}
}
StudentInfoByFront = new String[lineNumber];
lineNumber = 0;
for (int i = 0; lineNumber < StudentInfoByFront.length; i++) {
if (front.equalsIgnoreCase(StudentInfo[i][1])) {
StudentInfoByFront[lineNumber] = StudentInfo[i][0] + " "
+ StudentInfo[i][1] + " " + StudentInfo[i][2] + " " + StudentInfo[i][3];
lineNumber++;
}
}
return true;
}
public boolean SearchByName(String name) {
for (int i = 0; i < StudentInfo.length; i++) {
if (name.equalsIgnoreCase(StudentInfo[i][2])) {
ID = StudentInfo[i][0];
FrontName = StudentInfo[i][1];
Name = StudentInfo[i][2];
Lastname = StudentInfo[i][3];
return true;
}
}
return false;
}
public boolean SearchByLastname(String lastname) {
for (int i = 0; i < StudentInfo.length; i++) {
if (lastname.equalsIgnoreCase(StudentInfo[i][3])) {
ID = StudentInfo[i][0];
FrontName = StudentInfo[i][1];
Name = StudentInfo[i][2];
Lastname = StudentInfo[i][3];
return true;
}
}
return false;
}
public void show() {
if (ID != null) {
System.out.println("Data Found : " + ID + " " + FrontName + " " + Name + " " + Lastname);
}
else {
System.out.println("Data Found : " + StudentInfoByFront[0]);
for (int i = 1; i < StudentInfoByFront.length; i++) {
System.out.println("\t : " + StudentInfoByFront[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment