Hm... I think I may have been unclear in my question.
My code above looks for a period, exclamation point, or question mark as an indication that a sentence has ended. I will later add in extra tests for multiple punctuation marks (ex: "How cool is that??"), and it already occurred to me that I need to set up a separate
test for ellipses (...) because those don't always indicate the end of a sentence. Thank you, Campbell Ritchie, for pointing out another mid-sentence use of the period - I'll have to add a test for that.
However, my code can already find these punctuation marks:
What I need is a way to separate the original String input (a paragraph consisting of multiple sentences - the code does not know how many sentences) into an ArrayList, for which each element of the array is a StringBuffer whose contents are a single sentence.
In other words:
User input (a single String): "Hi, my name is Sammy. I am a Smith. I am Sammy Smith. Who are you?"
My code goes through the string, one character at a time (char c is the current character in the code above), and does two things:
- 1) The code keeps a counter of how many sentences there are in the paragraph (int numSen in the code above).
- 2) The code produces an ArrayList of StringBuffer elements:
myArrayList(0) = "Hi, my name is Sammy."
myArrayList(1) = "I am a Smith."
myArrayList(2) = "I am Sammy Smith."
myArrayList(3) = "Who are you?"
Where I need help is with part 2. Without knowing ahead of time how many sentences are in the paragraph/user input, how do I create a dynamic ArrayList with one StringBuffer element for each sentence?
I can't add a character to an element of the array (myArrayList(0).append(next character)) without first adding a StringBuffer to that element (myArrayList.add(newStringBuffer)).
However, I can't do this *before* the for loop starts because I don't know how many elements (sentences) I will need... and everything I've found online says I can't do this from *within* the for loop, because I can't set up an automatic new-variable-creating code (ex: the first time through the loop, it creates myStringBuffer1, the next time through it creates myStringBuffer2, then myStringBuffer3, etc., until the loop is done).
So my question is - is there another way I can code this to get the result I want (item 2 above)?