开发者

Searching Arraylist for a name and if not found return try again

I made a simple address book program using an Arraylist (program does not save entries after termination) and when I search the entries for an entry that was not the first one my error messages comes up with the entry information. I understand that this is happening because of the if condition used and the the error being an else. But I do not know how to make the program work another way. I want to be able search the entries for a specific name (String) and then display that persons contact info and if that name was not entered display "That person is not listed try another". I am very new to programming and have a very limited knowledge and I know my code looks bad right now but I would like to make it work correctly before making it look good.

                while(!search){
                    if(listSize == 0){
                        break;//does not allow search if no names have been entered.
                    }

                    System.out.print("Please enter the first name to search for or q to quit: ");
                    String searchName = in.next().trim().toUpperCase();

                    for(AddressBook a: myBook){
                    if(searchName.compareTo("Q") == 0){
                        search = true;//allows user to exit search
                    }

                    else if(a.getName().compareTo(searchName) == 0){
                        System.out.println("that contact info is: ");
                        System.out.println("name: " + a.getFullName());
       开发者_运维问答                 System.out.println("phone number: " + a.getPhoneNumber());
                        System.out.println("email: " + a.getEmail());
                        System.out.println("address: " + a.getAddress());
                        search = true;
                        break;
                    }

                    else
                        System.out.println("That name is not listed please try another");
                    }
                }

This is the search portion of my code, the problem I am having is with the last else if / else. I know that when I search for a name and it doesn't come up in the first element the program goes to the else because the else if condition is false and that is what I need to fix.


You made a logic error in your for statement. First, you should check for "Q" letter entered only once (not in for statement). Then you should check for existence of one of the names every iteration of the for, and then (if all items of myBook collection doesn't appears your search condition(equals to searchName), you should show the error message. So, improved code version looks like this:

while(!search){
   if(listSize == 0){
      break;//does not allow search if no names have been entered.
   }

   System.out.print("Please enter the first name to search for or q to quit: ");
   String searchName = in.next().trim().toUpperCase();

   if(searchName.compareTo("Q") == 0){
         search = true;//allows user to exit search
         break; //exit "while" iteration
   }

   boolean found = false;

   for(AddressBook a: myBook){
       if(a.getName().compareTo(searchName) == 0){
           System.out.println("that contact info is: ");
           System.out.println("name: " + a.getFullName());
           System.out.println("phone number: " + a.getPhoneNumber());
           System.out.println("email: " + a.getEmail());
           System.out.println("address: " + a.getAddress());
           search = true;
           found = true;
       }              
   }
   if(found == false)
       System.out.println("That name is not listed please try another");       
}


Did you try something like this:

System.out.print("Please enter the first name to search for or q to quit: ");
String searchName = in.next().trim().toUpperCase();
if (searchName.equals("Q") == false)
{
    boolean found = false;
    for(AddressBook a: myBook){ 
       if(a.getName().toUpperCase().equals(searchName)){
           System.out.println("that contact info is: ");
           System.out.println("name: " + a.getFullName());
           System.out.println("phone number: " + a.getPhoneNumber());
           System.out.println("email: " + a.getEmail());
           System.out.println("address: " + a.getAddress());
           found = true;
           break;
           }

    if(found == false)
    { 
       System.out.println("Item Not Found!");
    }
}

P.S. I think you should be using a HashMap since it is far more efficient if you just want to store and retrieve specific strings.

For a short tutorial, please follow this link.


Here is the flow you have now:

1. Read in the requested name to search for
2. Get the first element in the list. Put it in a.  
   a. Did the user enter Q? Then eventually exit the loop. 
   b. Does the first element match? If so, print info. 
   c. Otherwise, print The name is not listed. Try again...

What you want is

1. Read request
2. If request is Q, quit. 
3. If request is not Q then loop through the list
   a. If an element matches, set flag saying we found a match.
4. After the loop, check the flag to see if a match was found. If it was, print the info
5. If a match wasn't found, print Try again.

The long and short is, you will need to move some of your code out of the loop. I could write the code for you, but I'm sure you can get it. I will monitor this if you have more questions so I can help!


First of all, I would not use ArrayList - if you're going to search by name all the time, I would make it a Map with fullName as the key.

On the second case, why are you checking if searchNAme is "Q" in the Addressbook loop? You should do that instead of the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜