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

Thread: Paragraph count using scanner

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Paragraph count using scanner

    Hello,
    I'm trying to build a program that counts the number of paragraphs from a text file.I tried to use a scanner but when i run the program it returns me 0.
    I would appreciate any help...

    Here is my code:
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
     
    import javax.swing.JFileChooser;
     
     
    public class ParaCount {
     
    	public static void main(String[] args) {
     
     
    		JFileChooser chooseFile = new JFileChooser();
    		if (chooseFile.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
    			File selectedFile = chooseFile.getSelectedFile();
    			  Scanner sc=null;
    	    try
    	    {
    	        FileInputStream file = new FileInputStream(selectedFile);
    	        DataInputStream dis = new DataInputStream(file);
    	        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    	        sc = new Scanner(selectedFile);
     
    	       String str="";
    	       int paraCount = 0;
     
     
    	        while (sc.hasNextLine()){
    	            str = sc.nextLine();
    	            if (str.contains("\n") ){
    	                   paraCount++;
    	               }
    	            }
     	       System.out.println("Number of paragrahs:"+paraCount);
    	    } 
    	    catch(IOException e1){
    	        System.out.println(e1);   
    	    }
    	    }
    	}
    	}


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

    Default Re: Paragraph count using scanner

    Can you define what a paragraph is and how you recognize one so it can be counted?

    The newline character is used by the nextLine() to determine where the end of a line is. The newline character is not returned by the nextLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Paragraph count using scanner

    Quote Originally Posted by Norm View Post
    Can you define what a paragraph is and how you recognize one so it can be counted?

    The newline character is used by the nextLine() to determine where the end of a line is. The newline character is not returned by the nextLine() method.
    I'm not sure if a .txt document recognizes paragraphs so I arbitrarily define a paragraph as an empty space between two Strings.
    e.g.

    This is the first paragraph. This is the
    first paragraph.

    This is the second paragraph. This is the
    second paragraph.

    I realize that the problem is at the method nextLine().Is there a method that returns newline character?If not how else could I define a paragraph?

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

    Default Re: Paragraph count using scanner

    Scanner also uses spaces to delimit what it reads. You will not be able to see the spaces or newline characters using Scanner. You need to use a class and methods that reads the bytes of the file so you can detect the spaces and newline characters.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javajo32 (September 9th, 2012)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Paragraph count using scanner

    I finally used FileInputStream and the read() method recognizes newlines.

    Thank you very much!

Similar Threads

  1. split a paragraph
    By deepikamsc2010 in forum Collections and Generics
    Replies: 1
    Last Post: July 3rd, 2012, 09:25 AM
  2. [SOLVED] frequency count/Big Oh
    By edvir in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 25th, 2012, 07:24 AM
  3. Why does this code reset at every paragraph?
    By sungkhum in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2011, 07:36 PM
  4. How to Count the CAPS
    By Sean137 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2010, 08:30 AM
  5. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM