Skip to content

Instantly share code, notes, and snippets.

@mlabisi
Created July 29, 2016 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlabisi/a5f480d1700da857b1235c90a1906457 to your computer and use it in GitHub Desktop.
Save mlabisi/a5f480d1700da857b1235c90a1906457 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<Student>();
while (true) {
System.out.print("name: ");
String name = reader.nextLine();
if (!(name.isEmpty())) {
System.out.print("studentnumber: ");
String studentNumber = reader.nextLine();
students.add(new Student(name, studentNumber));
} else {
System.out.println("");
break;
}
}
for (Student student : students) {
System.out.println(student.toString());
}
System.out.println("");
System.out.print("Give search term: ");
String search = reader.nextLine();
for (Student student : students) {
String name = student.getName();
if (name.contains(search)) {
System.out.println("Result:");
System.out.println(student.toString());
}
}
}
}
public class Student {
// variables
private String name;
private String studentNumber;
// constructor
public Student(String name, String number) {
this.name = name;
this.studentNumber = number;
}
// methods
public String getName() {
return this.name;
}
public String getStudentNumber() {
return this.studentNumber;
}
public String toString() {
return this.name + " (" + this.studentNumber + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment