Results 1 to 3 of 3
- 12-10-2009, 01:25 AM #1
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 50
- Rep Power
- 0
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 12:05 PM. Reason: Grammar and mistakes in code
-
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.
- 12-10-2009, 12:06 PM #3
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
finding the number of computer connected in LAN
By sushil in forum NetworkingReplies: 1Last Post: 10-11-2009, 02:25 AM -
How to use Jtable to display File System as Total Commander?
By antiZzz in forum Advanced JavaReplies: 1Last Post: 01-16-2009, 05:44 PM -
finding length on a number
By thekrazykid in forum New To JavaReplies: 8Last Post: 12-12-2008, 08:07 PM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks