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 5 of 5

Thread: Searching

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Searching

    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
    public 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;
        }
    }
    Plz help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Searching

    Please explain what your problems are. Post the program's current output and explain what is wrong with it and what you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Searching

    It is not compiling, it is telling me actual and formal argument list differ in length. So I am not getting any out put. The program is suppose to find which List contains what I am searching for and if the List has it, it out prints the whole List.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Searching

    Please post the full text of any compiler errors.

    actual and formal argument list differ in length.
    When you call a method, you need to look at the definition for the method and provide the correct number of arguments to the method as shown in its definition.

    BTW The single letter a is a poor choice for the name of a variable. Give variables names with meaning so when you read the code you know what the variable contains and what it is used for.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching

    I ran it as well, and I should also state that you can't copy and paste this one out of the BlueJ compiler, which is what killer is using.
    Anyway,

    The problem is in the method binarySearch2, on this line:

    linearPrint(a.get(), low, toFind);

    You didn't put an int in the parentheses of a.get()

    I don't know which variable you want to print there, so I don't know what number to put there.

Similar Threads

  1. [SOLVED] Searching issues.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2012, 07:26 PM
  2. Searching for Graphics Designer
    By Ulixava in forum The Cafe
    Replies: 6
    Last Post: January 6th, 2012, 05:30 AM
  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