1

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?

4
  • 2
    so add break in if block? Commented Jun 29, 2021 at 2:57
  • the if block only checks if the name exists in an array it doesnt check if the name DOES NOT exist in the array Commented Jun 29, 2021 at 3:00
  • @AngeloMiguelLicsi The for loop checks if the name exists in the array. But you don't know until you check everywhere in the array. Commented Jun 29, 2021 at 3:01
  • Do the names need to be in an array? If you store them in a Set, you can check using the contains() method. Commented Jun 29, 2021 at 3:11

4 Answers 4

4

You can have a flag, which you can test after the loop

System.out.print("enter name")
name = sc.nextLine()

boolean found = false;

for (int count = 0; count < arrayofnames.length; count++)
{ 
    if (name.equals(arrayofnames[count]))
    { 
        found = true;
        break;
    }
}

if (!found) System.out.println ("Not found");
Sign up to request clarification or add additional context in comments.

1 Comment

i love you. this solved it. not really familiar with booleans lol
0
public static void main(String[] args) {
        String name = "jhn";
        String[] arrayofnames = {"john", "tomer", "roy", "natalie"};
        IsName(name, arrayofnames);

        
    }
    public static void IsName(String _name, String[] _arrayofnames){
        boolean a = false;
        for (String name : _arrayofnames) {
            if(name == _name){
                System.out.println("name is in the array");
                a = true;
            }
        }
        if(!a){
            System.out.println("name is not in the array");
        }
    }

Comments

0

You can solve this problem by putting your String[] arrayOfNames into an ArrayList<> and simply checking if the ArrayList<> contains the name given by the user.

String[] arrayOfNames = {"Angelo", "David", "John"};
String name = "Angelo"; //the name given by the user

ArrayList<String> newArray = new ArrayList<>(Arrays.asList(arrayOfNames));
//check too see if the ArrayList contains the name
if (newArray.contains(name))
    System.out.println("Name found in array");
else
    System.out.println("Name not found in array");

Comments

0

If you want your code more compact, you can also use Streams to check if any element of the array equals the given String like this:

boolean found = Arrays.stream(arrayofnames).anyMatch(n -> n.equals(name));

if(found) {
  //code to be executed if name is found in arrayofnames 
}

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.