完成一些搜索逻辑

时间:2013-12-19 15:53:43

标签: java regex

请帮我添加一些代码来完成逻辑。我有一些搜索方法(公共字符串搜索(String url,String someword)),它通过jsp title在内容中查找单词。所以这里有一个代码

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

我应该放在最后的“其他如果”以获得“ str =”未找到Word!“如果我输入搜索输入一些正常的字或数字,内容中缺少

2 个答案:

答案 0 :(得分:2)

此块

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

应该是这样的

将此代码移出for循环

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

答案 1 :(得分:0)

无需添加其他条件。但是,您的代码可以这样编写,因为您不需要在word循环中放置关于for的if语句:

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