Results 1 to 4 of 4
  1. #1
    Vox
    Vox is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Using Scanner to count paragraphs in a text file.

    Hello everyone. I have been trying to finish this homework assignment for a while now and I'm running into problems. I have to create a program that reads a simple text file and counts the number of words, sentences, lines, and paragraphs. The program then needs to output the results to another text file.

    I have the count for words, lines, and sentences working fine; but paragraphs are really stumping me. According to my professor: "a paragraph is basically a line with words followed by a line with nothing (or, if you wish, the other way around)."

    The first thing I thought of was to use scanner and feed the text into a string line by line, then search the string for \n or \r.

    Java Code:
    while (paraScan.hasNextLine()){
             line = paraScan.nextLine();
             if (line.contains("\n") || line.contains("\r")){
                    paraCount++;
                }
             }
    
    ...
    
    System.out.println("Paragraph count: " + paraCount);
    Technically, this would just count the number of lines; I just wanted to see if this code would recoginize \n or \r within the text file before I even attempt to write the actual code for paragraph count. The output is always Paragraph count: 0. This is very wrong, as there is at least 30-40 lines within my text document so I'm doing something wrong.

    Does anyone have any idea about how to write this? What it comes down to is I'm not sure about how to: search a line ("fed" into a string?) for a property and then, if said property is met, searchs the next line (string) for another property and if that one is met, increment the paragraph count.

    Thanks for any help you can provide.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    The problem is that nextLine() eliminates the \n's. An empty line will be read as an empty String, check for that.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Vox
    Vox is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    The problem is that nextLine() eliminates the \n's. An empty line will be read as an empty String, check for that.
    Thanks! I should have read the information on scanner.nextLine() better. The problem I have now is I'm not sure how to check the next line for content/words. I have tried a nested if statement like:

    Java Code:
        while (paraScan.hasNextLine()){
             line2 = paraScan.nextLine();
             if (line2.contains("") || line2.contains(null)){
             paraScan.nextLine(); // ???
                 if(line2 != ""){
                    paraCount++;
                 }
               }
             }
    but I'm getting an error on run: "java.util.NoSuchElementException: No line found." Obviously the problem is with the 2nd paraScan.nextLine(). How do I advance the scanner to the next line and check for content? Thanks again.
    Last edited by Vox; 03-22-2010 at 08:23 PM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Ow, you're supposed to compare Strings using equals() and the second paraScan.nextLine(); will fail if you have reached the last line. I'd remember the last line and if the last line is not empty, but the current line is empty you have found a paragraph boundary. But keep in mind that there could be an empty line at the end of the file.

    EDIT: and check whether the methods you invoke do what you think they do:
    Java Code:
    System.out.println("Hello Vox".contains(""));
    Last edited by PhHein; 03-22-2010 at 08:32 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 06:11 AM
  3. Use Scanner to parse text file, adding to HashMap
    By JordashTalon in forum New To Java
    Replies: 0
    Last Post: 03-05-2009, 12:08 AM
  4. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 08:37 AM
  5. Replies: 0
    Last Post: 04-06-2008, 08:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •