2 Replies - 328 Views - Last Post: 20 April 2014 - 07:34 AM Rate Topic: -----

#1 lovejava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 14

Counting the number of paragraphs in a text

Posted 19 April 2014 - 01:48 PM

Please help me figure out why my code wont compile the correct number of paragraphs


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;
			int paraCount = 1; 
			
			while (input.hasNextLine()) {
		
			String line;
			line = input.nextLine();
			lineNum++;
			wordCount += getWords(line);		
			charCount += (line.length());
			paraCount = getPara(line); 
			
			 

			}
			
			System.out.println(lineNum + "  line(s)"); 
			System.out.println(wordCount + " word(s)");
			System.out.println(charCount + " character(s)");
			System.out.println(paraCount);  
			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;

	}
	public static int getPara(String line){
		String str[] = line.split(( "\r\n"));  
		int count = 0; 
        if(str.length>1)
        {
            count++;
        }
		return count;


     
	}
}


This post has been edited by Martyr2: 19 April 2014 - 01:54 PM
Reason for edit:: Added code tags. Please use them in the future, thanks! :)


Is This A Good Question/Topic? 0
  • +

Replies To: Counting the number of paragraphs in a text

#2 satire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-April 14

Re: Counting the number of paragraphs in a text

Posted 19 April 2014 - 10:16 PM

I have edited the code to the version below, run it and lets hear your feedback.
Thank you.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ParagraphCount {

	public static void main(String[] args) {
		try {
			File file1 = new File("C:\\users\\codename_sam\\desktop\\data.txt");
			File file2 = new File("C:\\users\\codename_sam\\desktop\\data1.txt");
			
			Scanner sc = new Scanner(new FileReader(file1));

			PrintWriter output = new PrintWriter(new FileOutputStream(file2));
			
			

			int lineNum = 0;
			int wordCount = 0;
			int charCount = 0;
			int paraCount = 0;

			while (sc.hasNextLine()) {
				String line;
				line = sc.nextLine();
				lineNum++;
				wordCount += getWords(line);
				charCount += line.length();
				paraCount = getPara(line);

				if (!file2.exists()) {
					try {
						file2.createNewFile();
						output.print(line);
						output.flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
			
			System.out.println(lineNum + " lines");

			System.out.println(wordCount + " word(s)");

			System.out.println(charCount + " characters");

			System.out.println(paraCount);

			sc.close();
			output.close();

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

	private static int getPara(String line) {
		String[] str = line.split("\r\n");
		int count = 0;
		if(str.length > 1){
			count++;
			
		}
		return count;
	}

	private 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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Counting the number of paragraphs in a text

Posted 20 April 2014 - 07:34 AM

A 'next line' read with a Scanner has already been split on a line feed (or it wouldn't be a line)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1