Trying to use regex to match a word to a line of text and store the index of the first character of all matched words in a List to position an identifier ("^") on the next line after I print the matched line. I'm receiving the following error:
H:\CSCI 1152 notes>javac Project.java .\FindWord.java:17: error: incompatible types: String cannot be converted to List indexOfSearch = matcher.group(); ^
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());
}