0

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());
        }
Austin
  • 51
  • 1
  • 10
  • See if you can isolate the problem. Pasting a whole bunch of code makes it hard to figure out what you need. – theMayer Dec 02 '17 at 03:05
  • @theMayer I did condense it and thank you for the pointer. If it needs to be condensed further, I will try. – Austin Dec 02 '17 at 03:11
  • If you're trying to match text, you need to provide sample text, desired characteristics to match, and what you've tried. There is a lot more going on here than just searching for a match in a line of text. Keep in mind it is not necessary to paste your whole program - most of us are capable of setting up a main method, etc. – theMayer Dec 02 '17 at 03:13
  • @theMayer I do have sample text and characteristics within my question. My main method is not included. – Austin Dec 02 '17 at 03:15
  • From Help Center: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – theMayer Dec 02 '17 at 03:18
  • I am including desired behavior, a specific problem, what I have tried, sample text, and characteristics. – Austin Dec 02 '17 at 03:20
  • @theMayer I have attempted regex. How would I store the index of the pattern word once discovered via regex? Finding the word is no problem. – Austin Dec 02 '17 at 03:23
  • Regex returns a match object with details on where the match is in the string (at least that's how it works in c#, you'd have to research the Java implementation, but I'll bet it's very similar). See https://stackoverflow.com/questions/6020384/create-array-of-regex-matches – theMayer Dec 02 '17 at 03:24
  • 1
    Thanks for the link. It has helped me some, but I'm not there yet. The post is slightly different than my needs. However, it has allowed me to be more specific in my question because I have a better understanding of how to get there. – Austin Dec 02 '17 at 03:47

1 Answers1

1

You have a compiler error message:

error: incompatible types: String cannot be converted to List

on this line:

indexOfSearch = matcher.group();

group() returns a String, and indexOfSearch is defined as:

List<Integer> indexOfSearch = new ArrayList<>();

It is pretty clear this is a round peg in a square hole. You’re doing a couple of things wrong here. First, you are trying to assign to the List variable, when actually you want to add() to the List referred to by that variable. Secondly, the List is declared to hold Integer values, not String values, so you need to add something else, not matcher.group(). From your problem description, and the variable name, it looks like you want the index of the start() of the match.

indexOfSearch.add( matcher.start() );
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44