i have been asked to make phone book console application in java using collections/data structure

i have done some of it. i am having problem that i have to store firstname,lastname,cellnumber,emailaddress etc in such a way into collection so that if some one wants to search phone number from phonebook by first name "hassan" then program should return lastname,cellnumber,emailaddress for all first name which are"Hassan" .
my code goes here

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projectdimensional;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Vector;
import java.io.*;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
/**
 *
 * @author Faraz
 */
public class Main {
  public boolean isValidEmailAddress(String emailAddress) {
        String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = emailAddress;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();

    }
    public static boolean hasNumber(String s) throws IOException {

        Main m = new Main();
        for (int j = 0; j < s.length(); j++) {
            if (Character.isDigit(s.charAt(j))) {
                System.out.print("String Contains Digit");
                System.out.println("Not Allowed");
                System.exit(1);
            }
        }
        return true;

        //  return false;
    }

    public static boolean validateMobilePhone( String phone )throws IOException
     {

        if(phone.matches( "[0-9]\\d{2}-[0-9]\\d{3}-\\d{7}" ))
     {
      //System.out.println("MobileNumber");
     }
     else
     {
     System.out.println("Enter Number In right Formate i.e 092-0321-55279976");
     System.exit(1);
     }

     return true;
 }
public static boolean validateLandinePhone(String phone)throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int y;
 if(phone.matches( "[0-9]\\d{2}-\\d{7}" ))
     {
     // System.out.println("PhoneNumber");
     }
     else if(phone.contains(null))
     {
     System.out.println("Number Not Entered!! Do you wanna allow it null?? Enter 1 for yes 2 to No");
     //y=Integer.parseInt(br.readLine());

     System.exit(1);
     }
     else
     {
       System.out.println("Enter Number In right Formate i.e 051-55279976");
     }
return true;
}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        String firstName, lastName, middleName, homeAddress, emailAddress,mobileNumber,  residentialNumber;
        int ke;
        Main m = new Main();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList Key = new ArrayList();
        ArrayList Data = new ArrayList();

       // ArrayList Data2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map1.put("1", "faraz");

     // Iterator itra = map.keySet().iterator();
       // Iterator itr = map.values().iterator();

        try {
            System.out.println("How Much Contact Numbers You Want To ADD");
            int num = Integer.parseInt(br.readLine());
            for (int i = 0; i < num; i++) {
                System.out.println("Enter Sequence Id");
                ke = Integer.parseInt(br.readLine());

                if (Key.contains(ke))
                {
                    System.err.print("Record Already Found at this position");
                    System.exit(1);
                }
                else
                {
                    Key.add(ke);
                }
                Data.add("Details of ID"+"("+ke+")");
                System.out.println("Enter FirstName");
                firstName = br.readLine();

                hasNumber(firstName);
                Data.add(firstName);

                System.out.println("Enter LastName");
                lastName = br.readLine();
                hasNumber(lastName);
                Data.add(lastName);

                System.out.println("Enter MiddleName");
                middleName = br.readLine();
                hasNumber(middleName);
                Data.add(middleName);


                 System.out.println("Enter ResidentialNumber");
                 residentialNumber=br.readLine();
                 if(!(residentialNumber.isEmpty()))
                 {
                 validateLandinePhone(residentialNumber);
                 Data.add(residentialNumber);
                 }

                System.out.println("Enter MobileNumber");
                mobileNumber = br.readLine();
                if(!(mobileNumber.isEmpty()))
                {
                validateMobilePhone(mobileNumber);
                Data.add(mobileNumber);
                }
                System.out.println("Enter OfficeNumber");
                String officeNumbere=br.readLine();
               if(!(officeNumbere.isEmpty()))
               {
                validateLandinePhone(officeNumbere);
                Data.add(officeNumbere);
               }
                System.out.println("Enter HomeAddress");
                homeAddress = br.readLine();
                Data.add(homeAddress);
                System.out.println("Enter Email");
                emailAddress = br.readLine();
                if(!(emailAddress.isEmpty()))
                {
                if (m.isValidEmailAddress(emailAddress)) {
                    //System.out.println(emailAddress + "");
                    Data.add(emailAddress);
                }
                else
                {
                    System.out.println(emailAddress + " is an invalid email address.");
                    System.out.println("Run Application Again ");
                    System.exit(1);
                }
                }
                Data.add("!!!!!NEXTRECORD!!!!!");
            }
             map.put(Key,Data);
            System.out.println("Data" + map.values());


            System.out.println("Enter 1 to Search Record by first name");
            System.out.println("Enter 1 to Search Record by last name");
            System.out.println("Enter 3 to View Record");
            int option=Integer.parseInt(br.readLine());
            if(option==1)
            {
             System.out.println("Enter first name");
             String temp=br.readLine();
             for(int i=0;i<map.size();i++)
             {
             if(map.get(i).toString().equals(temp))
             {
              System.out.println("Value" + map.get(i)+"At position" + i);
             }

             }



            }

            System.out.println("For Keys" + map.keySet());
        } catch (Exception e)
        {
            System.out.println("Invalid Entry Please Run Application Again");
        }

    }

}

Kindly let me know how to fix it.

Recommended Answers

All 2 Replies

NoCodeTagsError at line 1, program terminated.
OutOfCheeseError at line 0, redo from start

1. Create a class Person with name, phone etc and the associated accessors and validation.
2. Create a class PhoneBook that contains a Collection of Persons, with methods to search / add a new Person etc
3. Create a class Test with a public main(...) method to create some Pesons, add them to an instance of PhoneBook, test the searches.
4. Never, repeat never, post code without code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.