views:

55

answers:

3

I have an array that I made that consists of first names. I have a search function that searches through the elements of the array and it works fine. However, for multiple elements of an array, I cannot find out how to print how many results were returned. For example, if "John" in my example is found, which it is, I do not know how to show that there were multiple results found. Someone please help me. I need to get "count" to increase 1 time for each result found. Here is my code: `

import java.util.*;

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

    Scanner search = new Scanner(System.in); 
       String[] firstName = new String[]{"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
       Arrays.sort(firstName);
       System.out.print("Enter a search term: ");
       String name = search.next();

       int i;

       boolean foundIt = false;

    search:
       for (i = 0; i < firstName.length; i++) {
           if (firstName[i] == name) {
                   foundIt = true;

               }
           }


       if (foundIt = true){
            System.out.println("Your search term of " + name + " produced " + count + " search results");
       }

       else {
           System.out.println("Your search term of " + name + " did not return any results");
       }
   }
}

'

+1  A: 

You could change the boolean foundIt to int count, and increment where you set foundIt to true.

So something like:

int count = 0;

search:
   for (i = 0; i < firstName.length; i++) {
       if (firstName[i] == name) {
               count++;
       }
   }


   if (count > 0){
        System.out.println("Your search term of " + name + " produced " + count + " search results");
   }

   else {
       System.out.println("Your search term of " + name + " did not return any results");
   }
Lachlan
Simple, concise and doesn't do anyone's homework for them! Perfect answer.
Bill K
Crap, I just added the code sample! Too much, Bill?
Lachlan
same here: don't use `==` for comparing Strings in Java!
Carlos Heuberger
A: 

Try this code :

    Scanner search = new Scanner(System.in);
    String[] firstName = {"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
    Arrays.sort(firstName);
    System.out.print("Enter a search term: ");
    String name = search.next();

    int count = 0;
    for (String aFirstName : firstName) {
        if (aFirstName.equals(name)) {
            count++;
        }else if(count > 0){
            break;
        }
    }

    if(count > 0){
        System.out.println("Your search term of " + name + " produced " + count + " search results");
    }else{
        System.out.println("Your search term of " + name + " did not return any results");
    }
Colin Hebert
A: 

You could add the results to an ArrayList, it's a little easier to understand then. Furthermore you assign the variable in the if statement and you use the == operator on strings, you should use .equals().

Another thing that stood out was the label in the code, you should stay away from them, since they require the use of the infamous break statement. See Dijkstra's considered harmful.

This way you can also iterate over the found names afterwards.

See the example:

import java.util.ArrayList;
import java.util.Arrays;    
import java.util.List;
import java.util.Scanner;


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

        Scanner search = new Scanner(System.in);
        String[] firstNames = new String[]{
            "John",
            "Thomas",
            "Samuel",
            "Chris",
            "Daniel",
            "Joey",
            "David",
            "Joshua",
            "Michael",
            "John"};

        Arrays.sort(firstNames);
        System.out.print("Enter a search term: ");
        String name = search.next();

        List<String> results = new ArrayList<String>();
        for (String s : firstNames) {
            if (s.equals(name)) {
                results.add(s);
            }
        }


        if (results.size() > 0) {
            System.out.println("Your search term of " + name + " produced " + results.size() + " search results");
        } else {
            System.out.println("Your search term of " + name + " did not return any results");
        }
    }
}
Jes