Hello,

I am new to programming and I have to write a program to perform a fast search on address book. The address book records are in csv file and contains 4 fields - first name, last name, phone number and company name. My problems are -
1. How do I connect the data on csv to the java program, means how do I import the address book, keeping all the fields in csv file linked to each other?
2. What data structure shall I use in java which is appropriate for an address book program?
3. I do not need to give any functionality to create a new record in address book, but I have to give a search functionality on all 4 fields. This would mean I have to have to keep the data sorted. How can I keep data sorted on all 4 fields? Or if I keep it sorted on 1 field only, which field shall I choose? How do I sort the data?

I don't even know how to begin. Can anybody show me some sample programs which does all this? Any help would be highly appreciated!

Thank you!

You should start and we will help you.
and about your questions:
1- I don't know how can we import this special file type (csv) and use its facilities but in general you can read the file by using FileInputStream class.

2- I think it is better to creat your own data structure containing the attributes first name, last name, phone number and company name and all their types are suitable to be string.

3-In order to sort the data your class should implement compareable interface and override compareTo method and define the style to compare your class instances.
then you can add your data to an array and use the class method

Arrays.sort(yourClassInstanceArray)

You just start your job and we will help you.
Good luck

Thank you so much for your encouraging words and help..as you advised, I'll start working on it and come back here for help!

Thank you again,
Priya

Hi again,

I have written this program but I have a few problems with it-
1. It is not returning me any result when searched on capital letter. For example, if I search on "a" it will list matches found, but when searching on "A" it says 0 matches found. I have tried IgnoreCase, toLowerCase, but probably I am not doing it right.
2. I have to demonstrate binary search algorithm with this program, along with display of time taken to find result, how do I do it?

 public void getResultsByFirstName(String pFirstName) throws IOException{
     long inTime;
     long totalTime;
     inTime = System.currentTimeMillis();
     ArrayList<String> records = new ArrayList<String>();
       String searchStr = null;
       String[] splitStr = null;
       searchStr = toLowerCase().pFirstName;
        for(int i=0; i <csvData.size();i++){
        splitStr = csvData.get(i).split(",");

        if(splitStr[0].equalsIgnoreCase().startsWith(searchStr)){
            records.add(csvData.get(i));
        }
        }
        System.out.println("Results: \n");
         for(int x=0; x < records.size();x++){
        System.out.println(records.get(x));
         }
        Collections.sort(records);
        int count = Collections.binarySearch(records, searchStr);
        int totalCount = count + (-2)*count -1;
        System.out.println("("+totalCount+ " total matches)");
        totalTime = System.currentTimeMillis() - inTime;
        System.out.println("Results fetched in: " +totalTime+ "ms");
        anotherSearchValidation();
      }

Would be thankful to anyone who can help me..

Regards,
Priya

Edited 3 Years Ago by pyTony: fixed formatting

This article has been dead for over six months. Start a new discussion instead.