Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Searching issues.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Searching issues.

    Back again! This program is supposed to search through an array of people and find who you're looking for based on a string given that can be their name, email, relation, birthday, or phone number. What I have come up on is that I used the exact same code for my findByName method (Which works fine) as I did for my findByEmail method, except I changed a couple of things to make it find the email and not the name. However, no matter what email I put in the string, it comes up as not finding it. Maybe you can see why? Here are my 2 classes:

    public class Contact
    {
       private String name, relation, bday, phone, email;
     
       public Contact(String n, String r, String bd, String p, String e)
       {
           name = n;
           relation = r;
           bday = bd;
           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 + "  " + relation + "  " + bday + "  " + phone + "  " + email;
        }
    }

    public class TestContact
    {
     
        public static void printContacts(List<Contact> a)
        {
            System.out.println("                  Contact List     ");
            System.out.println();  
            System.out.println("   Name           Relation         Birthday         Phone        Email");
            System.out.println("----------------------------------------------------------------------------------");
            for(Contact v : a)
            {
                System.out.println(v);
            }
            System.out.println();
        }
     
        public static void findByName(List<Contact> a, String s)
        {
            int top = a.size();
            int bottom = -1;
            int probe;
     
            System.out.println("Find Name - " + s);
            while (top - bottom > 1)
            {
                probe = (top + bottom) / 2;
                Contact g = a.get(probe);
                if(g.getName().compareTo(s) > 0)
                 top = probe;
                 else 
                 bottom = probe;
                }
                Contact bg = a.get(bottom);
                if ((bottom >= 0) && (bg.getName().compareTo(s) == 0))
                System.out.println("Found: " + bg);
     
                else
                System.out.println("There was no " + s + "found");
     
            }
     
            public static void findByEmail(List<Contact> a, String s)
            {
              int top = a.size();
              int bottom = -1;
               int probe;
     
              System.out.println("Find Email - " + s);
               while (top - bottom > 1)
              {
                probe = (top + bottom) / 2;
                Contact g = a.get(probe);
                if(g.getEmail().compareTo(s) > 0)
                 top = probe;
                 else 
                 bottom = probe;
                }
                Contact bg = a.get(bottom);
                if ((bottom >= 0) && (bg.getEmail().compareTo(s) == 0))
                System.out.println("Found: " + bg);
     
                else
                System.out.println("There was no " + s + " found");
            }
     
     
        public static void main(String [] args)
        {
            List<Contact> myContacts = new ArrayList<Contact>();
     
            myContacts.add(new Contact("John Carter", "brother", "Mar 3", "(342)555-7069", "jcarter@carter.com"));
            myContacts.add(new Contact("Elise Carter", "mom", "Apr 19", "(342)555-7011", "carterMom@carter.com"));
            myContacts.add(new Contact("Ellie Carter", "me", "Jun 10", "(342)555-8102", "ecarter@carter.com"));
            myContacts.add(new Contact("Sue Ellen", "friend", "Mar 9", "(341)555-9182", "susieE@hotmail.com"));
            myContacts.add(new Contact("Frank Carter", "dad", "Dec 1", "(342)555-7011", "carterDad@carter.com"));
            myContacts.add(new Contact("Johnnie", "friend", "Jan 21", "(341)555-7789", "jDawg5555@yahoo.com"));
     
            printContacts(myContacts);
            findByName(myContacts, "Johnnie");
     
            findByEmail(myContacts, "jcarter@carter.com");
            System.out.println();
     
     
        }
     
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Searching issues.

    The code in both findByName and findByEmail have some issues. For instance if you were to use: findByName(myContacts, "John Carter");, the contact would not be found. Basically your method does not check every contact which can be seen if you place System.out.println(g.getName()); right after Contact g = a.get(probe);. Doing that will allow you to see which contacts the loop is checking/skipping. Since you used an enhance for loop in the printContacts method already, I will suggest you use the same approach in findByName and findByEmail instead of the while loop. Here is an example of how to get out of an enhanced for loop once a condition is met:
    public class Test {
        public static void main(String[] args) {
            int[] intArray = {5,6,7,8,9,1,0,5,3};
            boolean found = false;
     
            for(int i : intArray) {
                if(i == 0) {
                    found = true;
                    System.out.println("0 was found in the array");
                    break; //This will break out of the for loop and move on to the next bit of code
                }
            }
            if(found == false) { // In case the for loop didn't find the integer
                System.out.println("0 was not found in the array");
            }
        }
    }
    Of course this uses an array and not a List, which would give some more options, but I think it will suffice.

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    Saintroi (April 28th, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Searching issues.

    Tried it out and it works great! Thanks a ton!

Similar Threads

  1. Searching for Graphics Designer
    By Ulixava in forum The Cafe
    Replies: 6
    Last Post: January 6th, 2012, 05:30 AM
  2. Sorting and Searching: Most Efficient Code
    By nicsa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2011, 11:20 PM
  3. Searching network for server
    By Jonathan_C in forum Java Theory & Questions
    Replies: 2
    Last Post: November 14th, 2010, 11:23 AM
  4. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 01:31 AM
  5. Searching Data
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 2nd, 2009, 03:23 AM