nooby nooby - 3 months ago 25
Java Question

Java how to see if a String contains multiple words?

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

}
}

Answer

Assuming that you use java 8, it would simply be:

try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt", true))) {
    System.out.println(line + " taken");
    if (!Arrays.stream(matches).anyMatch(html::contains)) {
        System.out.println(line + " is available or is only two chars");
        writer.write(line + "\n");
    }
}