1

I need to take a phrase that contains a specific word, then if it does have that word even if it's part of another word, to print the entire word out.

I think how to find the word "apple", but I can't figure how to find the word "appletree".

So far, I have some code that finds the word apple and prints that out.

String phrase = "She's sitting under an appletree!"; 

if (phrase.contains("apple")) {
    System.out.println("apple");
} else { 
    System.out.println("none"); 
}

How do I print "appletree"?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Possible duplicate of [Java: method to get position of a match in a String?](https://stackoverflow.com/questions/2615749/java-method-to-get-position-of-a-match-in-a-string) – racraman Jun 26 '19 at 23:50
  • If the phrase be coincident with the entire string, then you may use `if (phrase.contains("apple"))` – Tim Biegeleisen Jun 26 '19 at 23:51
  • I would do this by splitting the string into words, then looping through each **word** to check whether it contains "apple", instead of the whole phrase. As soon as a word contains apple, you can print the word and exit your loop. Try googling how to split a string by whitespace if you aren't sure how to split a string into words. – Keara Jun 26 '19 at 23:57
  • @StephenC I think I meant a string. And then to find out the rest of the phrase after the chose word. In which case I should have put a space between apple and tree, so it's two words. "She's sitting under an apple tree!" So how would I capture from apple all the way to ! ? – JackieTowns Jun 27 '19 at 00:44

3 Answers3

2

Use regex for a 1-liner:

String target = phrase.replaceAll(".*?(\\w*apple\\w*).*", "$1");

This works by matching (and thus replacing) the entire input, but capturing the target then using a backreference ($1) to the captured input resulting in just the target being returned.

The word in which apple appears is matched using \\w* (ie any number of word chars) at either end of apple. The minimum number of leading chars are matched outside the target by using a reluctant quantifier .*?, otherwise that expression would match all the way to apple, which would miss words like dapple.


Test code:

String phrase = "She's sitting under an appletree!";
String target = phrase.replaceAll(".*?(\\w*apple\\w*).*", "$1");
System.out.println(target);

Output:

appletree
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You could import a scanner to read the phrase. You would use the scanner.next() to capture each token of input into a String variable "s", in this case each word, and then use the if statement if(s.contains("apple")) then System.out.println(s).

Hope this helps!

  • Robin.
007Robin
  • 34
  • 1
  • 2
  • 9
0

without using regex you could simply split the sentence into words, loop through and check if it contains the requested word - old school style

String [] arr = phrase.split ("\\s+");
for (String word : arr) {
    if (word.contains("apple")) return word;
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64