0

When the array argi has more than one partition, its only begins to find word from the second url of the array, but why? Correct it please in code. Here the part of it: I make regular expresion for search by title in url:

private final Pattern TITLE = Pattern.compile("\\<title\\>(.*)\\<\\/title\\>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

And searh logic here:

public String search(String url, String someword) {

                    try {
                        InputStreamReader in = new InputStreamReader(new URL(url).openStream(),"UTF-8");
                        StringBuilder input = new StringBuilder();
                        int ch;
                        while ((ch = in.read()) != -1) {
                            input.append((char) ch);
                        }
                        if (Pattern.compile(someword, Pattern.CASE_INSENSITIVE).matcher(input).find()) {
                            Matcher title = TITLE.matcher(input);
                            if (title.find()) {
                                return title.group(1);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (PatternSyntaxException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

            String[] argi = {"http://localhost:8080/site/dipnagradi","http://localhost:8080/site/contacts"};

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

                                    String result = search(argi[i], word);

                                    if (result != null) {


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

                     }
                                    else{
                                        str="Search word not found!";
                                    }    

                                    if (word == null||word=="") {

                                        str = "Enter a search word!";


                                    }
                            }
                            return null;

                 }
Vit_B
  • 13
  • 3

2 Answers2

0

Use if (word == null || word.isEmpty()), (for beginners) never use == in Object comparison in java.

Also, for a more detailed answer, you really need to post your input and expected output. And also what`search() does.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • Yes, I have InputStreamReader and regular expression also in my code. So I changed as you write me, and in this case i got better situation, but anyway it doesnt find the word in first url of array..But if i write word in search which has both url of array, a search found it in both url..I need you understand i written – Vit_B Dec 10 '13 at 12:06
0

Do not use '==' operator with String as String is an Object and is compared using equals method.. Instead use : if(!"".equals(word){}

  • ok, thank you, i understand, but help me please also to finish logic because search doesnt find the word in first url of array..But if i write word in search which has both url of array, a search found it in both url – Vit_B Dec 10 '13 at 12:18