CS120 Project 2

Breaking a long line into sentences

Overview. Users may input text containing multiple sentences, and with extra spaces. In this project, you will write a program to break up a long line of multiple sentences into separate lines with one sentence each.

Technical details. Your program should prmpt for and read in one line of text via a call to a Scanner‘s nextLine method. Your program should break up the line, and print it back over several lines, with one sentence per line, each line in double-quotation marks.

  • A sentence ends with the end of the text input, or with one of three punctuation marks: a period (.), an exclamation point (!), or a question mark (?). But not every use of a punctuation mark ends a sentence:
    • Only periods followed by two or more spaces, or not followed by non-space characters in the input, should be understood as ending a sentence. Periods followed by zero or only one space should be understood as part of an abbreviation.
    • Exclamation points and question marks followed by one or more spaces, or at the end of the input string, should be seen as ending a sentence. Exclamation points and question marks followed by a non-space character do not end a sentence.
  • You should not print leading or trailing spaces as part of your sentences.
  • You should not print additional lines with “empty” sentences.

For this assignment, you can assume that your input contains only the normal printing characters you can enter from a standard US keyboard, including spaces, but not including tabs or any other “invisible” characters. In other words, the inputs I will use to test your program will include only spaces, letters, numbers, and punctuation marks. I will not use double-quotation marks, but I might use single-quotation marks. You should expect, and correctly handle, extra spaces at the very beginning and very end of the input.

You should use the charAt and substring methods of String to help you with your program.

Name your class SentenceFinder, and store it in a file SentenceFinder.java.

For example, on the input

I saw a mouse on St. Peter St.  The mouse roared loudly! And I ran away

(your browser may show this text on multiple lines, but it would be entered into your program on one single line), your program should print

"I saw a mouse on St. Peter St."
"The mouse roared loudly!"
"And I ran away"

Testing these assignments. I will write a program to test your work on several different input lines. Be sure to read in input exactly as described here, and to give only the expected output. Do not expect other interaction with the user except for what thie specification describes.

Submission instructions. Submit the single, plain-text file SentenceFinder.java to D2L by 8:30am on Monday, October 16. Remember to comply with the style guidelines for CS120 Java work.

Avoid plagiarism. The course syllabus has further details about our expectations of academic integrity. In particular: You may not copy code from any other source and claim it as your own. You may not share any of your code with any other student for use in completing any of these assignments.

Links to: CS120 homepage