-1

I am creating an address book in java and I am trying to implement the functionality of "searching" for a record. When I input a record and then try to search for it, it says that the record is not found, even though I put it in. Is there an additional step that I need to insert to correct this?

What I coded What the results where

I tried putting "String" next to the phoneNumber and even renaming it "phoneNumber1". Maybe it has to use an int for searching something specific?

Here is the collection that I am using

class CRUDDemo{
    public static void main(String[] args) {
        
        Collection<AddressBook> c = new ArrayList<AddressBook>();
        //to get input from user create an instance of scanner 
        //Scanner instance s is for numbers while Scanner instance s1 will be used for the string 
        Scanner s = new Scanner(System.in);
        Scanner s1 = new Scanner(System.in);
        int ch;
        do {
            //loop will run continuously until user provides an input
            System.out.println("1.INSERT");
            System.out.println("2.DISPLAY");
            System.out.println("3.SEARCH");
            System.out.println("4.DELETE");
            System.out.println("4.UPDATE");
            System.out.println("Enter Your Choice : ");
            ch = s.nextInt();
            
            switch(ch)

This first case is to display the the records that will be input from the user

case 1:
                System.out.println("Enter Name : ");
                String name = s1.nextLine();
                System.out.print("Enter Street : ");
                String street = s1.nextLine();
                System.out.print("Enter City : ");
                String city = s1.nextLine();
                System.out.print("Enter State : ");
                String state = s1.nextLine();
                System.out.print("Enter Zip : ");
                int zip = s.nextInt();
                System.out.print("Enter Phone Number : ");
                String phoneNumber = s1.nextLine();
                
                //add new object to the collection
                c.add(new AddressBook(name,street,city,state,zip,phoneNumber));
            break;

The second case will then allow for the input records to be displayed

case 2:
                //to separate out each record
                System.out.println("---------------------------------------------------------------");
                //Iterator is the function that will retract each record one by one 
                Iterator<AddressBook> i = c.iterator();
                //create a loop to see if next next record is available
                while(i.hasNext()) {
                    //this will display the records
                    AddressBook a = i.next();
                    System.out.println(a);
                }
                //draw a line to separate each record
                System.out.println("---------------------------------------------------------------");
            break;

Finally is my search option. I want to be able to search for a record in the address book. I have chosen to search by the person's phone number. If the record is there then I want it to just display the entire record. If it cannot find the record then it will say "Record not found"

case 3:
                //record cannot be found
                boolean found = false;
                System.out.println("Enter Phone Number to Search : ");
                phoneNumber  = s1.nextLine();
                //to separate out each record
                System.out.println("---------------------------------------------------------------");
                //create a loop to see if next next record is available
                i = c.iterator();
                while(i.hasNext()) {
                    //this will display the records
                    AddressBook a = i.next();
                    //create a loop so that phone number searched is equal to the one entered by the users in the record
                    if(a.getPhoneNumber() == phoneNumber) {
                        System.out.println(a);
                        found = true;
                    }
                }
                            
                if(!found) {
                    System.out.println("Record Not Found");
                }
                System.out.println("---------------------------------------------------------------");
            break;
            }
        }while(ch!=0);
    }
}

When I search for a record that has already been created from the user input, I get the message "Record not found". I wanted it to say this when there truly was no record found. Clearly I have made it say this statement all the time and I am unsure how to fix it.

  • Post text rather than images, wherever possible. – Basil Bourque Mar 16 '23 at 00:39
  • Your Question is quite vague and unfocused. Are you using a database? Are you using a [Java Collections](https://en.wikipedia.org/wiki/Java_collections_framework) data structure? Have you read the relevant parts of [The Java Tutorials](https://docs.oracle.com/javase/tutorial/) by Oracle before posting here? – Basil Bourque Mar 16 '23 at 00:41
  • I have updated my question. Apologies as I am a new developer and still learning how appropriately ask my questions in a way that makes the most sense to others. I will take note of this for next time. Thank you. – MStew_7627 Mar 16 '23 at 01:25
  • Change `if (a.getPhoneNumber() == phoneNumber) {` to `if (a.getPhoneNumber().equals(phoneNumber)) {` – Bohemian Mar 16 '23 at 02:55

1 Answers1

-1

Try this:

if (a.getPhoneNumber().equals(phoneNumber)){..}

and use Debug

Bohemian
  • 412,405
  • 93
  • 575
  • 722
AbrA
  • 434
  • 1
  • 6
  • 19