To be honest...I am horrible at java. I just got a job and have missed class a few times because of it and im supposed to create a project that can read the number of lines, words, characters, average words length, and letters in a file. I am trying to get the character reader to work right now but it tells me that I only get 1 character...

what am I doing wrong?

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class TextStatistics 
{

	int wordCount;
	int lineCount;
	int charCount;
	
	public void wordcounter(String filename) throws IOException, NullPointerException
		{
		
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    while ((file.hasNext()) )
		{
		String s = file.next();
		   wordCount++;
		}
	    
	    } catch (IOException e) {
	    }
			
			System.out.println("\n" + wordCount + " words read");
		}

	public void lineCounter(String filename) throws IOException, NullPointerException
		{
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    while ((file.hasNext()) )
		{
		String t = file.nextLine();
		   lineCount++;
		}
	    
	    } catch (IOException e) {
	    }
			
			System.out.println("\n" + lineCount + " lines read");
		}

	public void charCounter(String filename) throws IOException, NullPointerException
		{
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    StringTokenizer tokenizer = new StringTokenizer(filename, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n\t");
	    
	    while (tokenizer.hasMoreTokens())
		tokenizer.nextToken();
		   charCount++;
		}
	    
	   catch (IOException e) {
	    }
			
			System.out.println("\n" + charCount + " lines read");
		}
	
	@Override
	public String toString() {
		return "TextStatistics [chars=" + charCount + ", lines=" + lineCount
				+ ", words=" + wordCount + "]";
	}
	
	}

strange enough... when i run the app... it returns the correct number of words...

this is all i did

Yes. It got the words and the lines to count out correctly. (This is all without any special formatting or anything as you can see.)

the problem I am having currently is getting the characters to print out. Im trying to use a StringTokenizer except I am not sure how to get the string being read through the tokenizer to be the entire file instead of just "testfile.doc"

here is the main class:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class ProcessText 
{

	public static void main( String [] args) throws NullPointerException, IOException
    {
		for (int i=0; i<args.length; i++)
        {
            System.out.println("command line argument " + i + " = "+args[i]);
        }

	String first = args[0];
	TextStatistics fileZ = new TextStatistics ();
	fileZ.wordcounter(first);
	fileZ.lineCounter(first);
	fileZ.charCounter(first, "abdefghijklmnopqrstuvwxyz");
        System.out.println("Results: " + fileZ + " ");
    }


}

i dont know if you MUST use the tokenizer but this is far more easier...

public void charCounter(String filename) throws IOException, NullPointerException
		{
		File inFile = new File(filename);
	    try { Scanner file = new Scanner(inFile);
	    String contents = null;
	
	    while ((file.hasNext()) )
		{
		 contents += file.nextLine();
		 contents += "\n";
		}
	    
	    System.out.println("\n" + contents.length() + " Chars read");
   
	    }
	   catch (IOException e) {
		   System.out.println("An error Occured");
	    }
			
	 
		}

Note: mark as solved if this helped

Edited 6 Years Ago by phoenix911: n/a

Comments
Very helpful

Well he had told us to use the StringTokenizer, but due to time constraints I will go with that for now and proceed with the rest of my project.


Thank you very much. It now works.

Will mark as solved

To be honest...I am horrible at java. I just got a job and have missed class a few times because of it and im supposed to create a project that can read the number of lines, words, characters, average words length, and letters in a file. I am trying to get the character reader to work right now but it tells me that I only get 1 character...

what am I doing wrong?

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class TextStatistics 
{

	int wordCount;
	int lineCount;
	int charCount;
	
	public void wordcounter(String filename) throws IOException, NullPointerException
		{
		
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    while ((file.hasNext()) )
		{
		String s = file.next();
		   wordCount++;
		}
	    
	    } catch (IOException e) {
	    }
			
			System.out.println("\n" + wordCount + " words read");
		}

	public void lineCounter(String filename) throws IOException, NullPointerException
		{
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    while ((file.hasNext()) )
		{
		String t = file.nextLine();
		   lineCount++;
		}
	    
	    } catch (IOException e) {
	    }
			
			System.out.println("\n" + lineCount + " lines read");
		}

	public void charCounter(String filename) throws IOException, NullPointerException
		{
		File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    StringTokenizer tokenizer = new StringTokenizer(filename, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n\t");
	    
	    while (tokenizer.hasMoreTokens())
		tokenizer.nextToken();
		   charCount++;
		}
	    
	   catch (IOException e) {
	    }
			
			System.out.println("\n" + charCount + " lines read");
		}
	
	@Override
	public String toString() {
		return "TextStatistics [chars=" + charCount + ", lines=" + lineCount
				+ ", words=" + wordCount + "]";
	}
	
	}

Hey charlieruns,
see if this helps you,

File file = new File("PlayStrings.java");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

int charcount=0;

while(br.read()!= -1) // func read() returns the character read, or -1 if the end of the stream has been reached
{
charcount++;
}

System.out.println("The number of words in file are : "+charcount);
br.close();

This question has already been answered. Start a new discussion instead.