Click here to Skip to main content
15,999,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


What I have tried:

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]);
			}
		}
	}
Posted
Updated 11-Apr-18 8:40am

1 solution

And what is your question?

However, the assignment is
Quote:
the words that start with "pre".
but you use
Java
sTokens[i].endsWith("pre")

Java
System.out.println(printWordsWithPre(S1));
should throw a compilation error because your function printWordsWithPre() does not return anything (declared as void).
 
Share this answer
 
Comments
CPallini 12-Apr-18 7:37am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900