Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Writing Word, Sentence, Syllable Counters using Java

$
0
0
Hello,

My task was to write a java program which counts words, sentences and syllables. The program should read from a .txt file. The word and sentence count algorithms are pretty basic but I want to explain my method of counting syllables. In English, each vowel in a word increase the syllable count by one. If two vowels are together then instead of increasing the syllable count twice they only increase it once. Y is also included as a vowel. An e or y at the end of a word don't increase the syllable count.

My program compiles, however I get a NullPointerException when I run the main class. I couldn't understand what could be causing the problem. Thanks for your help!

 

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

public class FleschRI
{
  String line1 = "";
  String line2 = "";
  int wordcount = 0;
  int syllablecount = 0;
  int sentencecount = 0;
  BufferedReader inFile;
  
  
  StringTokenizer tokenizer;
  
  public FleschRI(String file) throws FileNotFoundException, IOException
  {
    
    BufferedReader inFile = new BufferedReader(new FileReader(file));
    
    while(true){
      
      
      
      line2 = (inFile.readLine()).toLowerCase();
           
      if (line2 == null)
        break;
      
      line1 = line1 + line2;
              
    }
   
     }
    
    public void wordcount() {
      StringTokenizer tokenizer = new StringTokenizer(line1);
      while(tokenizer.hasMoreTokens())
      {
        wordcount = tokenizer.countTokens();
      }
      System.out.println(wordcount);
    }
    
    public void sentencecount() {
      StringTokenizer tokenizer = new StringTokenizer(line1);
      while(tokenizer.hasMoreTokens())
      {
        String string = tokenizer.nextToken();
        char c = string.charAt(string.length() - 1);
        
        if (c == '.' || c == '?' || c == '!')
          sentencecount++;       
      }
      System.out.println(sentencecount);
      }
    public void syllablecount() {
      
      StringTokenizer tokenizer = new StringTokenizer(line1);
      
      while(tokenizer.hasMoreTokens())
      {
        int i, j;
        
        String string = tokenizer.nextToken();
        for(i = 0; i < string.length()-1; i++){
            char a = string.charAt(i);
        
        for(j = 1; j < string.length()-2; j++) {
            char b = string.charAt(j);
        if(a == 'a' || a == 'o' || a == 'e' || a == 'u' || a == 'i' || a == 'y' ) {
        syllablecount++;
        }
        
        if((a == 'a' || a == 'o' || a == 'e' || a == 'u' || a == 'i' || a == 'y' ) && 
           (b == 'a' || b == 'o' || b == 'e' || b == 'u' || b == 'i' || b == 'y' ))
        {
          syllablecount--;
        }
        if(string.charAt(string.length() - 1) == 'e' || string.charAt(string.length() - 1) == 'y')
        {
          syllablecount--; 
        }
        }
        }
        
      }
    }
    
    
}



My main class:
import java.io.*;

public class FleschRITester {
  
  public static void main(String args[]) throws FileNotFoundException, IOException{
    
    FleschRI article = new FleschRI("article.txt");
    
    article.wordcount();
    article.sentencecount();
    article.syllablecount();
    
  }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>