Results 1 to 3 of 3
  1. #1
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default Issue With Finding Total Number of Blank Spaces in File

    The goal of my assignment is to count the number of characters (w/o spaces and returns), words, and lines of a text document. The document is read-in and analyzed from there. I've got the reading in of the file working as well as the line counter; however, I've run into significant troubles with counting the characters. I can obtain the total number of characters, but I need to get a number without counting the spaces and returns. Here is what I've got thus far...

    Java Code:
    import java.io.*;
    
    public class Counter {
      public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        
        // Read in all variables
        String document;
        long characterCount = 0;
        int spaces = 0;
        int wordCount = 0;
        int lineCount = 0;
    
          // Ask user to enter document title with extenstion. Then convert file for finding required information.
          System.out.println("Please enter the file name with extension: ");
          document = br.readLine();
          File file = new File(document);
          FileReader fr = new FileReader(file);
          
    [COLOR="Red"]      // Find total number of characters within document
          characterCount = file.length();
          for (int i = 0; i < characterCount; i++) {
            if (document.charAt(i) == ' '); {
              spaces++;
            }
          }[/COLOR]
    
          characterCount = (characterCount - spaces);
          
    
          // Find total number of words within document
    
          
          // Find total number of lines within document
          LineNumberReader lnr = new LineNumberReader(fr);
          while (lnr.readLine() != null) {
            lineCount++;
          }
          
          // Print output of require information
          System.out.println("Number of characters in document: " + characterCount);
          System.out.println("Number of words in document: " + wordCount);
          System.out.println("Number of lines in document: " + lineCount);
        
      }
    }


    As far as the error message is concerned, it pertains to the portion of the code in red and reads:

    Java Code:
    [I]java.lang.StringIndexOutOfBoundsException: String index out of range: 8
    	at java.lang.String.charAt(Unknown Source)
    	at Counter.main(Counter.java:24)[/I]

    Any guidance would be greatly appreciated.
    Last edited by Cod; 12-10-2009 at 01:05 PM. Reason: Grammar and mistakes in code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    document is the file name. It's not all the text in the file. You're going to need to analyze each line one at a time as you read it in.

  3. #3
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    That make sense Fubarable, weird I didn't catch that. Thanks for pointint me in the right direction. Just got the program figured out this morning and everything is working smoothly.

    Thanks again.

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  2. finding the number of computer connected in LAN
    By sushil in forum Networking
    Replies: 1
    Last Post: 10-11-2009, 03:25 AM
  3. Replies: 1
    Last Post: 01-16-2009, 06:44 PM
  4. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 09:07 PM
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 09:14 AM

Posting Permissions

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