-3

Okay, I have made a phonebook that does the follows,

  1. Add new contact
  2. Edit contact's phone
  3. Delete contact
  4. Search by name
  5. Search by phone
  6. View all contact
  7. Restore factory settings
  8. Exit

I've finished it and it's working properly, except one thing. If a name is stored for example "John" and the user searches for "john", it will print "not found", because j is not capitalized, how can I fix this without screwing up really bad with my code.

Here's the code for case 2 for example.

case 2: {
    System.out.println("Enter the contact name you want to edit");
    temp=s.next();
    int z=name.indexOf(temp);
    if(z!=-1)
    {
        System.out.println("Edit to?");
        temp=s.next();
        name.set(z, temp);
        System.out.println("Name edited to "+temp);
    }
    else
        System.out.println("Name not found");
    break;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Heshamy
  • 69
  • 8
  • 1
    The easiest way would be to implement your own `indexOf` method and search using `equalsIgnoreCase` to search for the string. Or you could also override the arraylist's `indexOf` operation but I would consider it too much for this job. – Codebender Aug 31 '16 at 10:10
  • As my deleted answer reveals, there is no quick fix here. If you stored the names in all lowercase, then you could use something like `name.indexOf(temp.toLowerCase())`. But then you'd lose the case resolution in your stored data. – Tim Biegeleisen Aug 31 '16 at 10:11
  • YOu could use `.toLowerCase()` – Vogon Jeltz Aug 31 '16 at 11:02

2 Answers2

0

If you want all names to start with a capital letter you can only store them in this way, and then print in this way. The following code will help:

private String capitalize(String name) {
    String s1 = name.substring(0, 1).toUpperCase();
    String nameCapitalized = s1 + name.substring(1).toLowerCase();
    return nameCapitalized;
}

Use this method befor storing anything to your array and then ask for first occurence of capitalize(name);

In your case:

case 2: {
    System.out.println("Enter the contact name you want to edit");
    temp=s.next();
    int z=name.indexOf(capitalize(temp));
    if(z!=-1)
    {
        System.out.println("Edit to?");
        temp=s.next();
        name.set(z, capitalize(temp));
        System.out.println("Name edited to "+capitalize(temp));
    }
    else
        System.out.println("Name not found");
    break;
}

This approach will let you keep all names in the same convention - names starting with a capital letter.

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • It gives me an error when I call capitalize() "Cannot make a static reference to the non-static method capitalize(String) from the type Phonebook" – Heshamy Aug 31 '16 at 10:52
  • you have to add capitalize method to your class in which you have your switch/case. If you show the whole class in the question I'll tell you what exactly you have to do. – xenteros Aug 31 '16 at 10:52
  • @Heshamy make capitalize `public static` instead of `public`method – xenteros Aug 31 '16 at 10:54
  • Yeah I did that, added it right after "public class Phonebook {" – Heshamy Aug 31 '16 at 10:54
0
You can do like this. convert it into lowercase
case 2: {
System.out.println("Enter the contact name you want to edit");
temp=s.next();
int z=name.indexOf(temp.toLowerCase());
if(z!=-1)
{
    System.out.println("Edit to?");
    temp=s.next();
    name.set(z, temp);
    System.out.println("Name edited to "+temp);
 }
 else
    System.out.println("Name not found");
 break;
}
paresh
  • 351
  • 1
  • 3
  • 12