这个问题已经在这里有了答案:????????????>????????????Test if a string contains any of the strings from an array????????????????????????????????????12个
我的问题是,如何确定html字符串中是否包含任何内容,因此以下内容不起作用.它抛出不兼容的类型: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 + "
");
writer.close();
} else {
System.out.println(line + " taken");
writer.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
最佳答案
contains方法仅接受一个参数.其中必须有一个CharSequence.
您尝试改为传递字符串数组.
因此,一种简单的解决方案是将其更改为:
boolean anyMatch = false;
for (String match : matches) {
if (html.contains(match)) {
anyMatch = true;
然后再检查anyMatch.
记录:这真的很基本.编译器告诉您代码的确切问题是什么.当您收到编译器消息时…然后阅读它.阅读javadoc,了解正在调用的库方法.在网上搜索错误消息的一部分.这就是您解决此类问题的方法!