I really need help with this code...I have to display statistics from each and use Scanner and Tokenizer. For displaying each character I have to use Character.isLetter(). Please help me figure out how to display all of these things from a file.

//TextFileStats.java
//Print out Statistics using StringTokenizer.
import java.io.*;
import java.util.*;
import java.lang.*;

public class TextFileStats
{
public static Scanner scanner;
public static int wordCount;
public static int paragraphCount;
public static int charPerWordCount;
public static int sentenceCount;
public static int lineCount;
public static int charCount;

public static void main( String args[] ) throws IOException, NumberFormatException, FileNotFoundException
{
scanner = new Scanner( new File( "C:\\README.txt" ) );

while( scanner.hasNext() )
{
String input = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer( input );
lineCount++;

while ( tokenizer.hasMoreTokens() )
{

tokenizer.nextToken();
wordCount = 0;
wordCount++;

String character = tokenizer.nextToken();
boolean isCharacter = false;
int = 0;
while( i < tokenizer.length() && isCharacter )
{
String ch = tokenizer.substring( i, i + 1 );
if( ch.compareTo
charCount = Integer.parseInt( tokenizer.nextToken() );

charPerWordCount = Integer.parseInt( tokenizer.nextToken() );

sentenceCount = Integer.parseInt( tokenizer.nextToken() );

paragraphCount = Integer.parseInt( tokenizer.nextToken() );

}
}//end StringTokenizer
scanner.close();

//print out statistics
System.out.println( "Page count: " + lineCount/60 );
System.out.println( "Word count: " + wordCount );
System.out.println( "Character count: " + charCount);
System.out.println( "Character per word count: " + charPerWordCount );
System.out.println( "Sentence count: " + sentenceCount );
System.out.println( "Paragraph count: " + paragraphCount );
}
}

Recommended Answers

All 4 Replies

Isn't enough to place an incomplete question on the forum?
Do you have to spawn people with private messages?

So what is your question? Looking at what you quick typed without thinking, you are supposed to creare program that will read a text file, count ocurence of each character and provide statistics on how many times each character appeared in the text. For the purpose of your homework, you have to use Scanner, StringTokenizer and method isLetter(). I'm corect?

Yes that is correct. I need a program that will read in a file that will count each character, word, character per word, sentence, and paragraphs and I need to use StringTokenizer, Scanner, and Character.isLetter().

I know it isn't completely finished, that's why I need help, because I am somewhat clueless on which method I should choice.

I have completed most of my code, I just need help with counting how many paragraphs are on the text file.

//Stephen Kutner
//TextFileStats.java

import java.io.*;
import java.util.*;

public class TextFileStats
{
public static Scanner scanner;

public static int wordCount = 0;

public static int paragraphCount = 0;

public static int charPerWordCount = 0;

public static int sentenceCount = 1;

public static int lineCount = 0 ;

public static int charCount = 0 ;

public static String word = " ";

public static void main(String args[]) throws IOException, NumberFormatException, FileNotFoundException,
ArrayIndexOutOfBoundsException, NegativeArraySizeException
{

scanner = new Scanner(new File("C://README.txt"));
while (scanner.hasNext())
{
String input = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(input);
charCount = charCount + input.length();
lineCount = lineCount + 1;

while (tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
wordCount = wordCount + 1;

for( int i = 0; i < wordCount; i++ )
{
if( Character.isLetter('.') == true )
{
sentenceCount = sentenceCount + 1;
}
}

for( int j = 0; j < wordCount; j++)
{
if( Character.isSpace('\n') == true )
{
charPerWordCount = charPerWordCount + 1;
}
}
paragraphCount = paragraphCount + 1;
}
}// end StringTokenizer
scanner.close();

// print out statistics
System.out.println( "Page count: " + lineCount/60 );
System.out.println( "Word count: " + wordCount );
System.out.println( "Line Count: " + lineCount );
System.out.println( "Character count: " + charCount );
System.out.println( "Character per word count: " + charPerWordCount );
System.out.println( "Sentence count: " + sentenceCount );
System.out.println( "Paragraph count: " + paragraphCount );
}
}//end TextFileStats.java

Member Avatar for iamthwee

>I just need help with counting how many paragraphs are on the text file.

Ask yourself how you define a paragraph, then write the code to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.