•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,187 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,822 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1003 | Replies: 4
![]() |
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.*;
publicclass TextFileStats
{
publicstatic Scanner scanner;
publicstaticint wordCount;
publicstaticint paragraphCount;
publicstaticint charPerWordCount;
publicstaticint sentenceCount;
publicstaticint lineCount;
publicstaticint charCount;
publicstaticvoid 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 );
}
}
//TextFileStats.java
//Print out Statistics using StringTokenizer.
import java.io.*;
import java.util.*;
import java.lang.*;
publicclass TextFileStats
{
publicstatic Scanner scanner;
publicstaticint wordCount;
publicstaticint paragraphCount;
publicstaticint charPerWordCount;
publicstaticint sentenceCount;
publicstaticint lineCount;
publicstaticint charCount;
publicstaticvoid 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 );
}
}
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation:
Rep Power: 11
Solved Threads: 296
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?
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?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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 know it isn't completely finished, that's why I need help, because I am somewhat clueless on which method I should choice.
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.*;
publicclass TextFileStats
{
publicstatic Scanner scanner;
publicstaticint wordCount = 0;
publicstaticint paragraphCount = 0;
publicstaticint charPerWordCount = 0;
publicstaticint sentenceCount = 1;
publicstaticint lineCount = 0 ;
publicstaticint charCount = 0 ;
publicstatic String word = " ";
publicstaticvoid 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
//Stephen Kutner
//TextFileStats.java
import java.io.*;
import java.util.*;
publicclass TextFileStats
{
publicstatic Scanner scanner;
publicstaticint wordCount = 0;
publicstaticint paragraphCount = 0;
publicstaticint charPerWordCount = 0;
publicstaticint sentenceCount = 1;
publicstaticint lineCount = 0 ;
publicstaticint charCount = 0 ;
publicstatic String word = " ";
publicstaticvoid 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- sentenceCount and paragraphCount (Java)
- Java Expert (Needed) (Java)
Other Threads in the Java Forum
- Previous Thread: Switch Statement
- Next Thread: sentenceCount and paragraphCount



Linear Mode