Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to make sure my code is correct Ask the user to enter a product description as a sentence. If the user enters end

I want to make sure my code is correct

Ask the user to enter a product description as a sentence.

If the user enters "end" or "" then end the program. Otherwise, parse the sentence into its component words, and do the following checks:

- Check that the first word is capitalized and that the sentence ends with a period. If not display a message.

- Check for any capitalized words in the sentence, other than the first word, and display those as names.

- Check for any words in the sentence that end with a period, and display those as abbreviations.

- Check the words to see if any contain both an open parentheses and a close parentheses and if so, display the text between them as the product code.

Extra Credit:

When the above checks are complete, ask the user for the next product description sentence.

my code:

import java.util.Scanner; public class stringparsing { /** * * method that takes first word and last word in the sentence * prints status of first word starts with Capital letter * and last words ends with a period * * @param firstWord * @param lastWord */ public static void checkFirstWordCapital(String firstWord,String lastWord) { //System.out.println(" ************* IS FIRST WORD CAPITAL AND ENDS WITH PERIOD CHECK *************"); if(firstWord.length() > 0){ if(!Character.isUpperCase(firstWord.charAt(0))) { System.out.println(" First word does not starts with Capital Letter."); } }else { System.out.println(" First word does not contains any characters."); } if(lastWord.length() > 0) { if(lastWord.charAt(lastWord.length() - 1) != '.') { System.out.println("Product description sentence does not ends with a period"); } }else { System.out.println("Product description sentence does not ends with a period"); } } /** * method that takes * @param splittedWords * and prints all words which starts with capital letters as Names. */ public static void printNamesInTheSentence(String[] splittedWords) { //ignore the first word and start from 2nd word in the list of words. System.out.println(" ****************** Names in the Product Description Sentence ****************** "); boolean namesExists = false; for(int i = 1;i 0 && Character.isUpperCase(splittedWords[i].charAt(0))) { //print the name System.out.println(splittedWords[i]); namesExists = true; } } if(!namesExists) { System.out.println("No Names in the Product Description"); } } /** * method that takes * @param splittedWords * and prints all words which ends with a period as Abbreviations. */ public static void printAbbreviationsInTheSentence(String[] splittedWords) {

System.out.println(" ****************** Abbreviations in the Product Description Sentence ****************** "); boolean abbreviationsExists = false; //use for each loop to iterate the splitted words for(String word : splittedWords) { //if word contains characters and character at length - 1(Last Position) is period if(word.length() > 0 && word.charAt(word.length() - 1) == '.') { //then print it System.out.println(word); abbreviationsExists = true; } } if(!abbreviationsExists) { System.out.println("No Abbreviations in the Product Description"); } } /** * method that takes * @param splittedWords * * checks the words to see if any contains both an open parentheses and a close parentheses * and if so, display the text between them as the product code */ public static void printProductCodesInTheSentence(String[] splittedWords) {

System.out.println(" ****************** Product Codes in the Product Description Sentence ****************** "); boolean productCodesExists = false; //use for each loop to iterate the splitted words for(String word : splittedWords) { //get index of last open parenthesis index int openPareIndex = word.lastIndexOf('('); //get index of first closed parenthesis after above last opened parenthesis int closingPareIndex = word.indexOf(')', openPareIndex); if(openPareIndex != -1 && closingPareIndex != -1) { //then print the code between open and closed parenthesis System.out.println(word.substring(openPareIndex + 1, closingPareIndex )); productCodesExists = true; } } if(!productCodesExists) { System.out.println("No Product Codes in the Product Description"); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); String sentence; do { //prompt for senetence System.out.println(" Enter Product Description sentence: "); //get input from keyboard sentence = in.nextLine(); //if entered input is end or empty string(which contains length 0) if(sentence.equalsIgnoreCase("end") || sentence.length() == 0) { //break the loop break; } //split the sentence into words by space String[] splittedWords = sentence.split(" "); //if not continue //pass params as first word(located at index 0) and last word(located at index length-1) checkFirstWordCapital(splittedWords[0],splittedWords[splittedWords.length-1]); printNamesInTheSentence(splittedWords); printAbbreviationsInTheSentence(splittedWords); printProductCodesInTheSentence(splittedWords); //since we are breaking the loop if setnence is end or "" //while true is used as while condition. }while(true); in.close(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

=+ Are unions company-wide, regional, or national?

Answered: 1 week ago

Question

=+j Explain the litigation risks in international labor relations.

Answered: 1 week ago