Home > database >  Finding elements in array using for loop
Finding elements in array using for loop

Time:05-01

import java.util.Arrays;
import java.util.Scanner;

public class Grocer2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] names = new String[5];
        int count = 0;
        while(true){
            System.out.println("What is your name: ");
            // Store scanner input in name
            String name = scan.nextLine();
            // Add name into array
            names[count] = name;
            count  ;
            if(count == 5){
                break;
            }
        }
        System.out.println(Arrays.toString(names));
        while(true){
            System.out.println("Who are you looking for ? ");
            String contact = scan.nextLine();
            for(int i = 0; i < names.length; i  ){
                if(names[i].equals(contact)){
                    System.out.println("They are in aisle "   i);
                }else{
                    System.out.println("Not here");
                }
            }
            break;
        }
        scan.close();
    }
}

I am trying to add Scanner inputs into an array and I am trying to search for the element in an array using a for loop. The for loop looped through all the elements and print out "Not here" when names[i] is not equal to the Scanner input. How do I fix this issue ?

CodePudding user response:

while(true){
    System.out.println("Who are you looking for ? ");
    String contact = scan.nextLine();
    bool isFound = false;
    for(int i = 0; i < names.length; i  ){
        if(names[i].equals(contact)){
            System.out.println("They are in aisle "   i);
            isFound = true;
            break;
        }
    }
    if(!isFound){
        System.out.println("Not here");
    }
    break;
}

CodePudding user response:

If you want it to print not here only when you have not found the element at all you should put it after the for loop. So you would loop through the whole array if you find the name say that you found it and then if you didn't u can print not here so something like that :

     while(true){
        System.out.println("Who are you looking for ? ");
        String contact = scan.nextLine();
       boolean isThere=false;
        for(int i = 0; i < names.length; i  ){
             if(names[i].equals(contact)){
                 System.out.println("They are in aisle "   i);
                 isThere = true;
                 break;// break only if you want the first time the String appeared 
             }
         }
         if(!isThere){
          System.out.println("Not here");
         }
     break;
    }

Now that should work but here the while loop doesn't do anything. Consider removing it or doing something else with it since when you are doing the first loop you are breaking directly the first time so it's as if there were no loop at all.

CodePudding user response:

You can create a boolean called contactFound and set it to true when a matching name has been found. You may also want to break out of the loop when this happens. Outside of the for loop, print "Not here" if contactFound is false, as shown below.

boolean contactFound = false;
for (int i = 0; i < names.length; i  ) {
    if (names[i].equals(contact)) {
        System.out.println("They are in aisle "   i);
        contactFound = true;
        break; // If you want the loop to stop when a matching name has been found
    }
}
if (!contactFound) System.out.println("Not here");

CodePudding user response:

Your second while loop never loops. And you can use a do-while loop for the first loop. Here is a more compact version of the code:

    Scanner scan = new Scanner(System.in);

    String[] names = new String[5];
    int count = 0;
    do {
        System.out.println("What is your name: ");
        // Add name into array
        names[count] = scan.nextLine();
        count  ;
    } while (count != 5);

    System.out.println(Arrays.toString(names));

    System.out.println("Who are you looking for ? ");
    String contact = scan.nextLine();

    int aisle = -1;
    for(int i = 0; i < names.length; i  ){
        if(names[i].equals(contact)){aisle = i; break;}
    }

    if (aisle != -1) System.out.println("They are in aisle "   aisle);
    else System.out.println("Not here");
    scan.close();

Edit: Doing this is a lot easier with an ArrayList instead of dynamic arrays. Here is a version that uses ArrayLists:

    Scanner scan = new Scanner(System.in);

    ArrayList<String> names = new ArrayList<>(5);
    for (int i = 0; i<5; i  ){
        System.out.println("What is your name: ");
        names.add(scan.nextLine());
    }
    System.out.println(names);

    System.out.println("Who are you looking for ? ");
    String contact = scan.nextLine();

    if (names.contains(contact)) System.out.println("They are in aisle "   names.indexOf(contact));
    else System.out.println("Not here");
    scan.close();
  • Related