-1

I'm currently on a project where I'm supposed to create a phonebook in java with eclipse, I'm almost done but there's still one part of my code that I don't understand.

The following code sample is the part giving me some troubles, my condition if(...contains(...)) always answers true even if none of the contact have this part of String in their name. Also the whole contacts of the phone are displayed while I would like only the one that match.

I've tried to do with if(...contains(..) == true) but it's not working either.

On the other hand the second part looking for a matching with ID works perfectly and give back only the concerned "i". Using the same code but adapted to my String also doesn't produce any good result.

public static void display(){
    System.out.println("You selected display a contact, choose an option :");
    System.out.println("1. by the first name");
    System.out.println("2. by the last name");
    System.out.println("3. by the ID");
    System.out.println("4. by the phone number");

    Scanner sc = new Scanner(System.in);

    int str3 = sc.nextInt();

    if(str3 == 1){
        System.out.println("Please enter the first name of the contact");
        String fname = sc.nextLine();
        sc.nextLine();
        for(int i = 0; i <= (phoneBook.size()-1) ; i++){
            if(((phoneBook.get(i)).getFirstName()).toLowerCase().contains(fname.toLowerCase()) ){
                displayContact(phoneBook.get(i));
            }
        }}

    if(str3 == 2){
        System.out.println("Please enter the last name of the contact");
        String lname = sc.nextLine();
        sc.nextLine();
        for(int i = 0; i <= (phoneBook.size()-1) ; i++){
            if(((phoneBook.get(i)).getLastName()).toLowerCase().contains(lname.toLowerCase())){
                displayContact(phoneBook.get(i));
            }
        }
    }
    if(str3 == 3){
        System.out.println("Please enter the ID of the contact");
        int id = sc.nextInt();
        for(int i = 0; i <= (phoneBook.size()-1) ; i++){
            if(id == ((phoneBook.get(i)).getID())){
                displayContact(phoneBook.get(i));
            }
        }
    }

    if(str3 == 4){
        System.out.println("Please enter the phone number of the contact");
        int phnb = sc.nextInt();
        for(int i = 0; i <= (phoneBook.size()-1) ; i++){
            if(phnb == phoneBook.get(i).getPhoneNb()){
                displayContact(phoneBook.get(i));
            }

        }
    }
    System.out.println(" ");
    System.out.println("If you want to go back at main menu press 1, else press 2 to quit.");
    int str8 = sc.nextInt();
    if(str8 == 1){
        menu();
    }
    if(str8 == 2){
        quit();
    }

}
Jason
  • 1
  • 2

1 Answers1

0

I think the usage of Scanner, first reading a nextInt, and then twice a nextLine might cause an empty string, and that is always contained. You could take the second nextLine, or alternatively, use a BufferedReader, which is less error prone.

public static void display(){
    System.out.println("You selected display a contact, choose an option :");
    System.out.println("1. by the first name");
    System.out.println("2. by the last name");
    System.out.println("3. by the ID");
    System.out.println("4. by the phone number");

    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));

    int str3 = Integer.parseInt(sc.readLine());

    switch (str3) {
    case 1:
        System.out.println("Please enter the first name of the contact");
        String fname = sc.readLine();
        for (int i = 0; i < phoneBook.size(); i++) {
            if (phoneBook.get(i).getFirstName().toLowerCase().contains(fname.toLowerCase())) {
                displayContact(phoneBook.get(i));
            }
        }
        break;

    case 2:
        System.out.println("Please enter the last name of the contact");
        String lname = sc.readLine();
        for (int i = 0; i < phoneBook.size(); i++){
            if (phoneBook.get(i).getLastName().toLowerCase().contains(lname.toLowerCase())) {
                displayContact(phoneBook.get(i));
            }
        }
        break;

    case 3:
        System.out.println("Please enter the ID of the contact");
        int id = Integer.parseInt(sc.readLine());
        for (int i = 0; i < phoneBook.size(); i++) {
            if (id == phoneBook.get(i)).getID()) {
                displayContact(phoneBook.get(i));
            }
        }
        break;

    case 4:
        System.out.println("Please enter the phone number of the contact");
        int phnb = Integer.parseInt(sc.readLine());
        for(int i = 0; i < phoneBook.size(); i++){
            if(phnb == phoneBook.get(i).getPhoneNb()){
                displayContact(phoneBook.get(i));
            }
        }
        break;
    }
    System.out.println(" ");
    System.out.println("If you want to go back at main menu press 1, else press 2 to quit.");
    int str8 = Integer.parseInt(sc.readLine());
    if(str8 == 1){
        menu();
    }
    if(str8 == 2){
        quit();
    }
}

As an extra the switch statement.

Joop Eggen
  • 96,344
  • 7
  • 73
  • 121