开发者

Finding multiple search results in an array in Java

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");
       }
   }
}


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");
   }


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");
    }


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");
        }
    }
}


private void searchName(){

Employee[]e=toEmployee();
for(int i=0;i<em.size();i++){
    if(e[i].getName().equals(tfs.getText())){
        tfi.setText(e[i].getId());
        tfn.setText(e[i].getName());
        tfh.setText(e[i].getHour()+"");
        tfr.setText(e[i].getRate()+"");
    }
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜