3

My question is, how can I tell if the html contains anything in the string, the below doesn't work. it throws, incompatible types: java.lang.String[]

    public void run() {
        Document doc;
        String[] matches = new String[]{"Unavailable", "Too Short", "skin-3d"};
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt", true));
            //Thread.sleep(3000);
            doc = Jsoup.connect("https://t.com/s/" + line).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                    .referrer("http://www.google.com").ignoreHttpErrors(true).get();
            //Elements ID = doc.getElementsByClass("card-header");
            String html = doc.html();
            if (!html.contains(matches)) {
                System.out.println(line + " taken");
                System.out.println(line + " is available or is only two chars");
                writer.write(line + "\n");
                writer.close();

            } else {
                System.out.println(line + " taken");
                writer.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }
}
Aditya W
  • 652
  • 8
  • 20

2 Answers2

2

The contains method takes only one argument; which must a single CharSequence.

You try to pass an array of strings instead.

So one easy solution would be to change that to:

boolean anyMatch = false;
for (String match : matches) {
  if (html.contains(match)) {
   anyMatch = true;

and later on check anyMatch.

For the record: this is really basic. And the compiler told you exactly what the problem with your code is. When you get a compiler message ... then read it. Read the javadoc for the library methods you are calling. Search the net for parts of your error message. That is how you resolve such problems!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • This did not work for me, I am now getting refused connection since I am connecting three times. –  Aug 12 '16 at 09:02
  • Of course you have to make that change in place. And seriously: if such easy things give you so many problems: consider stepping back and learning some more basics right **now**. Don't play around with http connections, file readers and such things if you are unable to take my piece of example code and put that at the correct place in your source code. In other words: it is your code, you have to make sure that you calling connect() and html() just once! – GhostCat Aug 12 '16 at 09:11
0

From the Java API

boolean contains(CharSequence s)

Returns true if and only if this string contains the specified sequence of char values.

You cannot give an array of String to the contains method but one String. I suggest you to do a for loop to solve your problem:

boolean match = false;
for (String s : matches){ 
    if (html.contains(s)){
        match = true;
        break;
    }
}
L01c
  • 1,033
  • 10
  • 19
  • Your answer is the same as GhostCat answer, if you have nothing new, then no need to have a duplicate answer. Just you can vote up the answer that you think is right for this question. Thanks – Bahramdun Adil Aug 12 '16 at 09:04
  • First of all, I was writing my answer at the same time. Secondly I think the reference to the Java API is really important. It shows people that researches can be made upfront asking a question and it often the best way to find answers. Finally my solution seems to be a bit quicker as it stops the loop once any element found. – L01c Aug 12 '16 at 09:10
  • If only for stopping loop you re-answered the question, then it was better to edit the first answer or leave a comment, anyway there is nothing new in your answer just copying and pasting. – Bahramdun Adil Aug 12 '16 at 09:14
  • Please read my comment again. Thanks. – L01c Aug 12 '16 at 09:17
  • I have read your comment, you are the best and the most profession person in Java in the world. Just keep on copy and paste, good luck for you! – Bahramdun Adil Aug 12 '16 at 09:19
  • Lack of arguments. It ends here. – L01c Aug 12 '16 at 09:21