0

I'm trying to break a text into its various paragraphs. I did find this question and this question. However, I've already figured out how to detect the paragraphs. I'm having trouble saving them.

One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin.  He lay on
his armour-like back, and if he lifted his head a little he could
see his brown belly, slightly domed and divided by arches into stiff
sections.  The bedding was hardly able to cover it and seemed ready
to slide off any moment.  His many legs, pitifully thin compared
with the size of the rest of him, waved about helplessly as he
looked.

"What's happened to me?" he thought.  It wasn't a dream.  His room,
a proper human room although a little too small, lay peacefully
between its four familiar walls.  A collection of textile samples

The text above would be counted as two paragraphs. Below is the function that I am using for paragraph detection.

public List<Paragraph> findParagraph(List<String> originalBook)
{
    List<Paragraph> paragraphs = new LinkedList<Paragraph>();
    List<String> sentences = new LinkedList<String>();


    for(int i=0;i<originalBook.size();i++)
    {
        //if it isn't a blank line
        //don't count I,II symbols
        if(!originalBook.get(i).equalsIgnoreCase("") & originalBook.get(i).length()>2)
        {
            sentences.add(originalBook.remove(i));

            //if the line ahead of where you are is a blank line you've reach the end of the paragraph
            if(i < originalBook.size()-1)
            {
                if(originalBook.get(i+1).equalsIgnoreCase("") )
                {
                    Paragraph paragraph = new Paragraph();
                    List<String> strings = sentences;
                    paragraph.setSentences(strings);
                    paragraphs.add(paragraph);
                    sentences.clear();
                }
            }
        }

    }

    return paragraphs;
}

And this is the class that defines my Paragraph

public class Paragraph
{

    private List<String> sentences;

    public Paragraph()
    {
        super();
    }


    public List<String> getSentences() {
        return sentences;
    }

    public void setSentences(List<String> sentences) {
        this.sentences = sentences;
    }

}

I'm able to detect the paragraphs fine, but I'm clearing all of the sentences and I'm getting a list that only contains the last paragraph. I've been trying to think of a solution and I haven't been able to come up with one. Can anybody offer any advice?

I've tried to be as thorough as possible in my explanation. I can add more details if necessary.

Community
  • 1
  • 1
j.jerrod.taylor
  • 1,120
  • 1
  • 13
  • 33

2 Answers2

2

The issue is in this block:

Paragraph paragraph = new Paragraph();
List<String> strings = sentences; // <-- !!!!!
paragraph.setSentences(strings);
paragraphs.add(paragraph);
sentences.clear();

You use the same object that sentences points to for all your paragraphs, so in the end all your Paragraph objects will point to the same List<String>. Thus, any change you make to sentences will alter that single List<String>, and the changes will be seen across all your Paragraph objects, as they all refer to the same instance.

It's a little like if sentences were a balloon, what you're doing is giving all your Paragraph objects a string leading to that balloon (plus another string leading back to sentences). If one of those objects (or the sentences reference) decides to follow the string and pop the balloon, everyone will see the change.

The solution is simple. Skip sentences.clear() and simply use List<String> strings = new LinkedList<>() instead of List<String> strings = sentences. That way, all your Paragraph objects will have distinct List<String> objects that hold their sentences, and changes you make to any one of them will be independent of the other. If you do that, you can skip declaring sentences at the beginning of the method too.

awksp
  • 11,764
  • 4
  • 37
  • 44
0

You can change your code to be more efficient and clean, rather than calculating its index and creating multiple if statements.

sample:

Scanner scan = new Scanner(new File("text.txt"));
String parag = "";

while(scan.hasNextLine())
{
    String s = scan.nextLine();
    if(s.trim().length() != 0)
        parag += s + "\n"; //new sentence
    else
    {
        System.out.println(parag); //new paragraph
        parag = "";
    }
}

System.out.println(parag); //last paraggraph
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63