1

so i had a exam to day and while i found a way to make my code work i dont like it

    public static void search(String name, Friend[] array) {

    for (int i = 0; i<array.length;i++) {

        if((array[i].getName()).equals(name)) {
            System.out.println(name+ " is found at position " +i+"\n");
        }
        else {
            System.out.print("\nName not in list\n");
        }
    }
}

So what i do here works, im searching the array of type Friend for a name ive passed from the main method. But i want to stop when it finds a unique name, so while i like what i have as it shows if there is more than one of the name i would like to show just the ones say that contain John and ignore every other name or if there is no John that it would just print a single "Name not in list"

4 Answers 4

2

You can add a break and also have a boolean to avoid printing the same message over and over:

boolean nameFound = false;
for (int i = 0; i<array.length;i++) {
    if((array[i].getName()).equals(name)) {
        System.out.println(name+ " is found at position " +i+"\n");
        nameFound = true;
        break;
    }
}

if(!nameFound) System.out.print("\nName not in list\n");
Sign up to request clarification or add additional context in comments.

6 Comments

Good Stuff Buddy, Is there a way to work it that it would display both if it happened that both names entered were John
Hey @AidanWard, what do you mean by "both names entered"?
say i entered two names both which happened to be John, then once the loop finds the first John it will exit which would be incorrect as the second object is also called John
I got it, but your method is receiving only one name String name, if you want to find more names, you'll need to change your method signature, you got me?
@AidanWard If you want to print a message for every "John" in your array, simply remove the break; line and there you go :)
|
2

You have two problems here:
you don't break the for loop after you found the name
You print the 'not found' message on the else part, and not after the for loop ended, that's why you get it for each friend

public static void search(String name, Friend[] array) {
    for (int i = 0; i<array.length;i++) {
        if((array[i].getName()).equals(name)) {
            System.out.println(name+ " is found at position " +i+"\n");
            return; // Stop if you found one
        }
    }
    System.out.print("\nName not in list\n"); // print that only after going through the entire list

}

Comments

0

Use break to break your loop once the condition is met.

public static void search(String name, Friend[] array) {
   boolean found = false;
   for (int i = 0; i<array.length;i++) {
      if((array[i].getName()).equals(name)) {
         System.out.println(name+ " is found at position " +i+"\n");
         found = true;
         // break the loop
         // it will throw the control out of the loop
         break;
       }
    }

    // Not found print name not in the list
    if(!found){
       System.out.print("\nName not in list\n");
    }
 }

1 Comment

you will still print Name not on the list for every wrong name
0

Just an alternative for the fun of it, if you don't care about the index. It also returns whether the name was found.

public static boolean search(String name, Friend[] array) {

     boolean found = Arrays.stream(array)
               .anyMatch(item -> item.getName().equals(name));

     if (found) {
        System.out.println(name+ " is found\n");
     }
     else {
       System.out.print("\nName not in list\n");
     }

     return found;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.