我有一些问题。在这里,我想在 line.contains() 中使用 string[],之前我尝试了一个代码,如果我放入 line.contains(String),它会读取关键字,但我一次只能输入一个关键字。因此,我尝试将关键字分组到一个数组中,但主要问题是 line.contains() 无法读取 String[]。
这是代码:-
package components;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class matchWord {
public static void main(String[] args) {
File file = new File("file.txt");
String[] errorType = {"uuid=A5H50AV_promo1, for domain=null", "Start node"};
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException ex) {
System.out.println("File not found");
}
int count = 0;
//now read the file line by line
for (int i = 0; i < errorType.length; i++) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//it doesn't read the errorType[i] as i can see that the count=0
if (line.contains(errorType[i])) {
count++;
if (count == 1) {
System.out.println("Error Description :" + line);
}
}
}
}
System.out.println("Error Type : " + errorType + "\nCount : " + count);
scanner.close();
}
}
有人请帮忙,非常感谢。