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 2 of 2

Thread: counting number of paragraphs in a text

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

    Default counting number of paragraphs in a text

    How do I count the number of paragraphs using a separate method?
    Thank you!


    import java.util.*;
    import java.io.*;

    public class WordStats1 {

    public static void main(String[] args) {
    try {

    Scanner input = new Scanner(new FileReader("data.txt"));

    PrintWriter output = new PrintWriter(new FileOutputStream(
    "newfile.txt"));

    int lineNum = 0;
    int wordCount = 1;
    int charCount = 0;

    while (input.hasNextLine()) {

    String line;
    line = input.nextLine();
    lineNum++;
    wordCount += getWords(line);
    charCount += (line.length());


    }

    System.out.println(lineNum + " line(s)");
    System.out.println(wordCount + " word(s)");
    System.out.println(charCount + " character(s)");
    input.close();
    output.close();

    System.out.print("File written.");

    }

    catch (FileNotFoundException e) {
    System.out.println("There was an error opening one of the files.");
    }
    }

    public static int getWords(String line) {
    String str[] = line.split((" "));
    int count = 0;
    for (int i = 0; i < str.length; i++) {
    if (str[i].length() > 0) {
    count++;

    }

    }
    return count;

    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: counting number of paragraphs in a text

    Cross posted here.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly. Once you see how at the link I've provided, you can edit your post with the tags of your choice.

    Also, add details about your question. What's wrong with the method you've written? If you're getting errors, post those. If you're getting incorrect results, show a sample run and describe what's wrong with it and ask how to improve it.

Similar Threads

  1. Count number of paragraphs in a document
    By Versatile in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 28th, 2014, 07:13 AM
  2. Number Counting Array issues
    By compileDammit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2013, 08:12 PM
  3. [SOLVED] counting number of comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 26th, 2012, 11:54 PM
  4. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  5. java code to split text documents into paragraphs and sentences
    By draksha in forum Java Theory & Questions
    Replies: 5
    Last Post: August 17th, 2011, 05:06 AM