2010-08-26 82 views
1

我有一個由名字組成的數組。我有一個搜索功能,搜索數組的元素,它工作正常。但是,對於數組的多個元素,我找不到如何打印返回的結果數。例如,如果找到我的例子中的「John」,那麼我不知道如何顯示找到多個結果。有人請幫助我。我需要「計數」來爲每個找到的結果增加1次。這裏是我的代碼: `在Java中查找數組中的多個搜索結果

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

不使用''==在Java中比較字符串!如果兩個字符串都是相同的實例,這將只返回true,用'equals'來比較字符串的內容。看到http://stackoverflow.com/questions/767372/java-string-equals-versus – 2010-08-27 09:51:08

回答

1

你可以改變的布爾foundIt爲int計數,並增加你設置foundIt爲true。

因此,像:

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

簡單,簡潔,並沒有爲他們做任何人的功課!完美的答案。 – 2010-08-26 20:03:48

+0

垃圾,我只是添加了代碼示例!太多了,比爾? – Lachlan 2010-08-26 20:05:22

+0

同樣在這裏:不要使用'=='比較Java中的字符串! – 2010-08-27 09:46:56

0

試試這個代碼:

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

您可以在結果添加到一個ArrayList,這是一個比較容易理解即可。此外,您在if語句中分配變量,並且在字符串上使用==運算符,則應使用.equals()

另一件突出的事情是代碼中的標籤,你應該遠離它們,因爲它們需要使用臭名昭着的break聲明。請參閱Dijkstra的considered harmful.

這樣,您也可以在之後迭代找到的名稱。

見例如:

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