2013-12-19 107 views
1

請幫我添加一些代碼來完成一個邏輯。我有一個搜索方法(公共字符串搜索(字符串url,字符串someword)),它通過jsp標題在內容中查找單詞。因此,這裏代碼完成一些搜索邏輯

String[] argi = {"http://www.google.com", "http://www.youtube.com"}; 

     for (int i = 0; i < argi.length; i++) { 

      String result = search(argi[i], "\\b"+word+"\\b"); 

       if (word == null || word.isEmpty()) { 

         str = "Enter a search word!"; 
      } 
       else if(!word.matches("^\\w+$")){ 

         str="Incorrect word!"; 
      } 

       else if (result != null) { 

         str += "Search phrase " + "<b>"+ word + "</b>" + " have found in " + "<a href=\"" + argi[i] + "\">" + result + "</a>"+ "<p></p>"; 

     } 
else if (????????) {str="Word not found!";} 
     } 
} 

要我穿什麼在最後的「否則,如果」獲得「海峽=」字找不到!「如果我輸入要搜索輸入一些正常的字或數字,這是在內容不存在

+1

沒有。只要把'else'。 – Maroun

+0

爲什麼不只是沒有條件? – Kiwy

+1

@Kiwy可能有我的評論的重複;) – Maroun

回答

0

沒有必要添加其他條件但是,您的代碼可以這樣寫,因爲您不需要將if語句放入在for環內的210:

String[] argi = {"http://www.google.com", "http://www.youtube.com"}; 
String str = ""; 

if (word == null || word.isEmpty()) { 
    str = "Enter a search word!"; 
} 
else if(!word.matches("^\\w+$")) { 
    str = "Incorrect word!"; 
} 
else { 
    for (int i = 0; i < argi.length; i++) { 
     String result = search(argi[i], "\\b" + word + "\\b"); 
     if (result != null) { 
      str += "Search word <b>" + word + "</b> have been found in " 
       + "<a href=\"" + argi[i] + "\">" + result + "</a><br>"; 
     } 
    } 
    if (str.isEmpty()) { 
     str = "Word not found!"; 
    } 
} 
+0

也許最後「如果」你的意思 - if(result == null || result.isEmpty())? – Jonny

+0

@Jonny:不,因爲如果'str'是空的,那是因爲沒有找到任何東西,因爲這個詞不是空的和正確的。 –

+0

@Jonny:如果你在這裏測試'result'的值,你只測試'result'的最後一個值。如果在第一個'argi'中找到'word',但在第二個中沒有找到'word',您將獲得「Word not found!」 –

2

此塊

else if (????????) {str="Word not found!";} //Remove this line 

應該是這樣的

移動此代碼出來的for循環

for (int i = 0; i < argi.length; i++){ 
} 
str="Word not found!"; 
+0

不,它不工作..我已經嘗試..然後它找不到任何單詞甚至在內容 – Jonny