Question: When I type something which isn't there in array list, "Name Doesn't Match" is printing for 5 times. I want to make this print for only one time.

1. Which line of code is making "Name Doesn't Match" print 5 times?
2. how to print "Name Doesn't Match" only 1 time?

```
public class Arrays {
    public static void main(String[] args) {

String[] names = {"Meisam", "Raju", "Sasi", "Aju", "Ram"};
        int[] numbers = {123456, 654321, 345678, 953456, 123445};
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }

        System.out.println("Please Enter a Name:  ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();

        for(int i = 0; i < names.length; i++){
            if(name.equals(names[i])) {
                System.out.println(numbers[i]);
            } else {
                System.out.println("Names Doesn't Match!");
            }
            }
        }
    }

```

Output is:

```
Meisam
Raju
Sasi
Aju
Ram
Please Enter a Name:  
raj //input a keyword called 'raj' 
Names Doesn't Match!
Names Doesn't Match!
Names Doesn't Match!
Names Doesn't Match!
Names Doesn't Match!

```

Answer: 
> 
> Which line of code is making "Name Doesn't Match" print 5 times?
> 
> 
> 

```
for(int i = 0; i < names.length; i++) {
   // ...
}

```

It will print `Names Doesn't Match!` for each element from the array. If the array contains e.g. 10 elements, then you'll see 10 time this message.

> 
> how to print "Name Doesn't Match" only 1 time?
> 
> 
> 

Just check the whole array first and set `a marker` where the name found or not. Only after that if `maker is false`, print the message.

```
int j = -1;    // j == -1 -> means NOT_FOUND

for (int i = 0; i < name.length(); i++) {
    if (name.equals(names[i])) {
        j = i;
        break;
    }
}

System.out.println(j == -1 ? "Names Doesn't Match!" : numbers[j]);

```

---

I think it's better to use `Map` for this issue:

```
public static void main(String... args) {
    Map<String, Integer> nameNumber = Map.of(
            "Meisam", 123456,
            "Raju", 654321,
            "Sasi", 345678,
            "Aju", 953456,
            "Ram", 123445);

    nameNumber.keySet().forEach(System.out::println);

    System.out.print("Please Enter a Name:  ");
    String name = new Scanner(System.in).next();
    Integer number = nameNumber.get(name);
    System.out.println(number == null ? "Names Doesn't Match!" : number);
}

```
```java
for(int i = 0; i < names.length; i++) {
    if(name.equals(names[i])) {
        System.out.println(numbers[i]);
    } else {
        System.out.println("Names Doesn't Match!");
    }
}

```

Is making your code print the string 5 times. It does it, because you are iterating over all 5 elements of your `names` array.

I would suggest, if you want it to print once, you can put it in a function and return upon a matched result on return `"Names Doesn't Match!`