I am having to write a program to search for Strings in a List and if it finds it, the program needs to out print the List. I have been trying to figure this out but I cant.
here is my tester class
import java.util.*; public class TesterContact { public static void printContact(List<Contact> a) { System.out.println("Name Relation BrithDay Phone Email"); System.out.println("------------------------------------------------------------------------------------"); for(int i = 0; i < a.size(); i++) { System.out.println(a.get(i)); } } public static void sortName(List<Contact> a) { int i; int k; int posmax; Contact temp; for(i = a.size() - 1; i >= 0; i--) { posmax = 0; for(k = 0 ; k <= i ; k++) { if(a.get(k).getName().compareTo(a.get(posmax).getName()) >= 0) posmax = k; } temp = a.get(i); a.set(i, a.get(posmax)); a.set(posmax, temp); } } public static void binarySearch2(List<Contact> a, String toFind) { int high = a.size(); int low = -1; int probe; while ( high - low > 1 ) { probe = ( high + low ) / 2; if ( a.get(probe).getName().compareTo(toFind) > 0) high = probe; else { low = probe; if ( a.get(probe).getName().compareTo(toFind) == 0) { break; } } } if ( (low >= 0) && (a.get(low).getName().compareTo(toFind) == 0 )) { linearPrint(a.get(), low, toFind); } else System.out.println("Not found: " + toFind); } public static void main(String[] args) { List<Contact> myContact = new ArrayList<Contact>(); myContact.add(new Contact("Jonh Carter", "brother", "Mar 3", "(342) 555-7069", "jcarter@carter.com")); myContact.add(new Contact("Elise Carter", "mom", "Apr 19", "(342) 555-7011", "caterMom@carter.com")); myContact.add(new Contact("Elise Cater", "me", "Jun 10", "(342) 555-8102", "ecarter@carter.com")); myContact.add(new Contact("Sue Ellen", "friend", "Mar 9", "(341) 555-9182", "susieE@hotmail.com")); myContact.add(new Contact("Frank Carter", "dad", "Dec 1", "(342) 555-7011", "carterDad@carter.com")); myContact.add(new Contact("Johnnie ", "friend", "Jan 21", "(341) 555-7789", "jDawg5555@yahoo.com")); sortName(myContact); printContact(myContact); System.out.println(); searchName(myContact, "John Carter"); } }
and here is my constructor
Plz helppublic class Contact { String name; String relation; String bday; String phone; String email; public Contact(String n, String r, String b, String p, String e) { name = n; relation = r; bday = b; phone = p; email = e; } public String getName() { return name; } public String getRelation() { return relation; } public String getBday() { return bday; } public String getPhone() { return phone; } public String getEmail() { return email; } public String toString() { return name + "\t" + relation + "\t" + "\t" + bday + "\t" + "\t" + phone + "\t" + email; } }