0

The way it works is, that the user will paste a text into the cmd and then he can choose one of the program options. One of them is find specific word/phrase in text. This is the code I have for this section:

    public void searching() {
    
    System.out.println("Enter the word you wish to find");
    Pattern pattern = Pattern.compile(scan.next());
    Matcher matcher = pattern.matcher(text);
    
    boolean found = false;
    while (matcher.find()) {
          System.out.println("I found the text "+pattern+" starting at index "+    
             matcher.start()+" and ending at index "+matcher.end());    
            found = true;    
        }    
        if(!found){    
            System.out.println("No match found.");    
    }
}

My problem is that if the user's text is for example "lorem ipsum dolor sit amet" and they want to find where the word "dolor" is, the output is "no match found". The program will only find the word lorem if the user asks for it.

The other question is: is there a way to make the output be not the index of the found word but the whole text and the found word in the upper case?

  • This might help: https://stackoverflow.com/questions/5091057/how-to-find-a-whole-word-in-a-string-in-java – Ava_Katushka Sep 29 '20 at 12:40
  • For your purpose you just can skip regexp completely and just use "user text input here".contains("the word") – Ava_Katushka Sep 29 '20 at 12:43
  • Print `text` before putting it in the matcher, and validate, that `text == "lorem ipsum dolor ..."`. I think your value differs from your expectation. – TreffnonX Sep 29 '20 at 12:45

1 Answers1

2

You're doing it. Your code, as pasted, works fine. Self-contained test-case:

Pattern pattern = Pattern.compile("dolor");
Matcher matcher = pattern.matcher("lorem ipsum dolor sit amet");
boolean found = false;
while (matcher.find()) {
  System.out.println("I found the text "+pattern+" starting at index "+    
    matcher.start()+" and ending at index "+matcher.end());    
  found = true;    
}    
if(!found) {
  System.out.println("No match found.");    
}

and it prints:

I found the text dolor starting at index 12 and ending at index 17

You can use the index to then write some code and manipulate the string. You're looking for:

  • substring - slices strings up. "hello".substring(1, 4) becomes `"ell".
  • toUpperCase() - uppercases text. "hello".toUpperCase() becomes "HELLO"
  • concatenate strings. Just + will do. "he" + "ll" + "o" becomes "HELLO"

These operations are all you need.

first make 3 substrings: before the 'matcher.start()', between start and end, and after end. Then uppercase the middle one. Then concatenate them all. If you want to uppercase all the matches, similar techniques. Maybe you want to involve StringBuilder which lets you build up the string piece by piece:

StringBuilder x = new StringBuilder();
x.append("he");
x.append("ll");
x.append("o");
String y = x.toString();
System.out.println(y); // prints "hello"
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72