Печатайте слова с пре в них java
Create a class StringDemo that contains the following static methods: • A method that takes in a sentence, tokenises it using a single space, and then prints only the words that start with "pre". • The main method where you declare 2 variables and initialise them to sentences of your choice; then test the method that you have defined in previous step using the 2 sentences.
Что я уже пробовал:
import java.util.ArrayList; public class StringDemo{ public static void main(String... args){ String S1 = "We love prefix that have car strongly wheels and car seats"; String S2 = "I have a lovely designed car that has many car features beautifully and the car has a good car"; printWordsWithPre(S1); printWordsWithPre(S2); System.out.println(printWordsWithPre(S1)); System.out.println(printWordsWithPre(S1)); } //- Tokenises a string/sentence and prints only the words that end with ly. public static void printWordsWithPre(String str){ String[] sTokens = str.split("\\p{javaWhitespace}"); for(int i = 0; i < sTokens.length; i++){ if(sTokens[i].endsWith("pre")){ System.out.println(sTokens[i]); } } }