Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter a sentence. NEED TO USE MATCHES METH

ID: 3528509 • Letter: W

Question

Write a program that asks the user to enter a sentence. NEED TO USE MATCHES METHOD to validate that the entered sentence consists only of letters and ends with either: a period, an exclamation mark, or a question mark. If it is not a valid sentence, your program should output that the input is not valid. Otherwise the input should be displayed with all the vowels replaced with an @ character (see example in lecture notes). The following shows sample runs of the program: Run #1 enter a sentence that consists of letters only and ends with a period, an exclamation mark, or a question mark: The first sentence is not valid The entered input "The first sentence is not valid" is not a valid. Run #2 enter a second sentence that consists of letters only and ends with a period, an exclamation mark, or a question mark : The 2nd sentence is also invalid! The entered input "The 2nd sentence is also invalid!" is not a valid. Run #3Please enter a sentence that consists of letters only and ends with a period, an exclamation mark, or a question mark: It is a Good day The entered input: "It is a Good day." with all vowels replaced is: "@ t @s @ G@@d d@y."

Explanation / Answer

Hi, Here is the code using matcher. Hope this gives you desired output. import java.util.*; public class Test { public static void main( String[] args) { String str; Scanner in = new Scanner(System.in); System.out.println("Enter a sentence that consists of letters only and ends with a period, an exclamation mark, or a question mark:"); str = in.next(); String str1 = str; String[] result = str.split("\a"); int a=0; Pattern p = Pattern.compile("[0-9]"); Matcher m = p.matcher(str); if (m.find()) a = 1; if (a =0){ Pattern q = Pattern.compile("[.!?]"); Matcher n = q.matcher(result[result.length]); if (n.find()) a = 0; else a=1; } if (a =0){ str.replaceAll( "[aeiouAEIOU]", "@" ); System.out.println("""+str1+"" with all vowels replaced is: ""+str+"""); } else{ System.out.println("""+str1+"" is not a valid"); } } } Thanks,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote