정규 표현식을 사용하여 단어를 텍스트 행에 일치시키고 일치하는 모든 단어의 첫 문자 색인을 목록에 저장하여 다음에 식별자 ("^")를 배치하십시오 일치하는 줄을 인쇄 한 후목록에서 패턴 일치 색인을 저장하십시오.
H : \ CSCI 1152 노트> javac의 Project.java \ FindWord.java :. 17 : 오류 : 호환되지 않는 유형 : 문자열 목록에 변환 할 수 없습니다 indexOfSearch = matcher.group (I 다음과 같은 오류 메시지가 표시됨); 이 라인에
error: incompatible types: String cannot be converted to List
: ^
public String findWords(String str, String search) throws Exception {
try {
List<Integer> indexOfSearch = new ArrayList<>();
Pattern pattern = Pattern.compile("\\b" + search + "\\b");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
indexOfSearch = matcher.group();
}
String fullCarets = "";
System.out.println(str);//print the text from file
if(indexOfSearch.size() >= 1) {
for (int j = 0; j <= indexOfSearch.size() - 1; j++) {//each index of search word
String spaces = "";//spaces to caret
int indexTo = 0;//how many spaces will be added
if (j < 1 && indexOfSearch.get(j) != -1) {
indexTo = indexOfSearch.get(j);//the first index
} else if (indexOfSearch.get(j) != -1) {
indexTo = (indexOfSearch.get(j) - indexOfSearch.get(j - 1) - 1);//all other indexes in the row
}
if (indexTo >= 0 && indexOfSearch.get(j) != -1) {
for (int i = 0; i < indexTo; i++) {//add appropriate number of spaces to word
spaces += " ";//add a space
}
fullCarets += (spaces + "^");
System.out.print(spaces + "^");//print the spaces and spaces
}
}
System.out.println("");//used to make the print slightly easier to look at.
return str + "\n" + fullCarets + "\n";
}
return "";
}catch (Exception e) {
throw new Exception(e.getMessage());
}
문제를 격리 할 수 있는지 확인하십시오. 모든 코드를 붙여 넣으면 필요한 항목을 파악하기가 어려워집니다. – theMayer
@theMayer 나는 그것을 응축 시켰고 포인터에 감사드립니다. 추가로 응축해야하는 경우 시도해 보겠습니다. – Austin
텍스트를 일치 시키려면 샘플 텍스트, 일치시킬 원하는 특성 및 시도한 것을 제공해야합니다. 텍스트 줄에서 일치하는 항목을 검색하는 것보다 훨씬 많은 작업이 진행됩니다. 대부분의 사람들은 메인 프로그램 등을 설정할 수 있습니다. – theMayer