The output i need
enter name: john
name not found in array
Here is my pseudocode
//ask for name
System.out.print("enter name")
name = sc.nextLine()
//loop to check if name exists in my array
for (int count = 0; count < arrayofnames.length; count++)
{
if (name.equals(arrayofnames[count]))
{ code to be executed if name is found in arrayofnames }
}
I tried putting an else/else if condition after the if but it executes the code arrayofnames.length times. How do I only make it print something like "name not found in array" only once then break?
breakinifblock?forloop checks if the name exists in the array. But you don't know until you check everywhere in the array.Set, you can check using thecontains()method.