Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Count number of paragraphs in a document

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Count number of paragraphs in a document

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class Paragraph
    {
        public static void main(String[] args) throws FileNotFoundException, IOException 
    {
     
        BufferedReader readfile = new BufferedReader(new FileReader("output.txt"));
        String line = readfile.readLine();
        int count = 0;
        StringBuilder paragraph = new StringBuilder();
        while (true) {
            if (line==null || line.length() == 0) {
                count++;
                //System.out.println("paragraph " + count);
                //sparagraph.setLength(0);
                if(line == null)
                    break;
            } else {
                paragraph.append(" ");
                paragraph.append(line);
            }
            line = readfile.readLine();
        }
        readfile.close();
        System.out.println("Number of paragraphs: "+ count);   
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Count number of paragraphs in a document

    Did you have a question?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Count number of paragraphs in a document

    Quote Originally Posted by Norm View Post
    Did you have a question?

    Yes Norm, I did have a question.
    I m not getting whats wrong with my code in calculating number of paragraphs in a document. For every input its giving answer as zero.
    Please help me with this.
    Thanks in advance.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Count number of paragraphs in a document

    Try debugging the code by adding some println() statements that print out the values of variables as their value changes. For example the value of line.
    That will show you what the computer sees when it executes the code and help you understand why the code is doing what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Count number of paragraphs in a document

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding some println() statements that print out the values of variables as their value changes. For example the value of line.
    That will show you what the computer sees when it executes the code and help you understand why the code is doing what it does.
    Thanks Norm, i tried printing value of line which results null everytime. I am really confused what to do

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Count number of paragraphs in a document

    If the value of line is null after the first read, then is the file empty?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Count number of paragraphs in a document

    Are you sure you have compiled the above-posted code? Unless it was a copy-paste mistake, it's missing a close curly brace right at the end of the code listing to close off the class. Once the close curly brace is added, even with a completely empty output.txt file you'll get a "Number of paragraphs: 1", or a FileNotFoundException if output.txt is absent. Therefore it may be that it isn't compiling, and you have been testing with an earlier compiled but incomplete version of your code.

    If this is not the case, please post your code that has println debug statements added, and attach a zipped copy of output.txt (in case there are hidden control characters messing things up).

Similar Threads

  1. Code will not count number of links
    By HudgensTyler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 2nd, 2014, 04:08 AM
  2. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  3. Replies: 3
    Last Post: January 28th, 2011, 09:37 AM
  4. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 02:50 AM
  5. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 03:28 PM