Simple Letter Counter Debugging

Greetings, I'm trying to write a program that counts the letters in sentence. the catch is i want it to be able to take in a paragraph and display the output for every sentence in the paragraph... someone in this jumbled web of code im having a run time error and cant for the life of me get it straight... can someone here please help me straighten this out so i dont have to go threw and restructure the rest of my program to work around this...

Sincerely, Danielcc
import javax.swing.*;
 * @author danielcc
public class LetterCounterSecond {
//this counts the periods
public static int countPeriods(String input){
	int numbOfPeriods=0;
	for(int index=0 ; index < input.length() ; index++){
		if (input.charAt(index)=='.'){
	return numbOfPeriods;
}//end of period counter
//this is the length of the paragraph
public static int lengthOfParagraph(String input){
	int length=input.length();
	return length;
}//end lengthOfParagraph
//this makes an array out of the paragraph for probing
public static char[] parragraphArray(String aInput){
 	String input=aInput.toLowerCase();
	char[] parragraphArray=new char[lengthOfParagraph(input)];
	for(int index=0;index<lengthOfParagraph(input);index++){
	return parragraphArray;
}//end of parragraphArray
//this takes the indexes of the periods in the paragraph starting at the first period
//array will look like this [0,5,10,25]... note the 0 as a place holder used later 
//to mark the begging of first sentence by substrings
public static int[] indexOfPerriods(String input){
	int[] indexOfPeriods=new int[countPeriods(input)];
	for(int index=0; index<countPeriods(input); index++){
		//must use i because index is taken
		char[] parragraphArray= parragraphArray(input);
			for(int i=1; i<(countPeriods(input)+1);i++){ 
	return indexOfPeriods;
}//end of indexOfPeriods
public static String[] arrayOfSentences(String input){
	int[] indexOfPeriods=indexOfPerriods(input);//note 0 is 0 and periods start on 1
	//declairing the array to hold arrays...
	String[] sentenceArray=new String[countPeriods(input)];
	//note this first for statement is new code and should be looked over
	for(int counter=0;counter<indexOfPeriods.length;counter++){
		for(int index=0;index<sentenceArray.length;index++){
		}//end of for statement using array length
	}//end of first for statement using counter in conjunction with # of periods aka length of period array
	return sentenceArray;
}//end of arrayOfSentences
public static int[] letterTally(String input, int sentence){//sentence starts at 0
	int[] periodIndex=indexOfPerriods(input);
	for(int i=0; i<periodIndex.length; i++){
		System.out.println("LetterCounterSecond.letterTally: periodIndex["+i+"] is: " + periodIndex[i]);
	input=input.substring(periodIndex[sentence],periodIndex[(sentence+1)]);//changed this from sentence,sentence++
	//String[] arrayOfSentences=arrayOfSentences(input);
	char[] inputArray=new char[input.length()];
		for(int index=0;index<input.length();index++){
	//this is a corresponding array for letters with 0 as a and equal to 0 1 is b and equal to one and so forth
	int[] letterCountArray= new int[25];
	for(int index=0;index<inputArray.length;index++){	
			//could use switch but a for loop would not require typing out long case scenarios		
			//switch(inputArray[index]){//letterArray is the letters with corresponding numbers ie 0 is a
			for(int i=0; i<26 ; i++){
				if (inputArray[index]==i){ 
					letterCountArray[index]=letterCountArray[index];//note letterCountArray is equal to the number of letter per the corresponding letterArray
			}//end of for statement
		}//end of switch
	return letterCountArray;
}//end of letterTally
//this basicly brings everything together and prints the output	
public static void printResults(String input){
	for(int sentence=0;sentence<countPeriods(input);sentence++){
		int[] letterCountArray=letterTally(input,sentence);
		JOptionPane.showMessageDialog(null, "sentence: "+(sentence+1));
		JOptionPane.showMessageDialog(null, "a: "+letterCountArray[0]+"\nb: "+letterCountArray[1]+"c: "+letterCountArray[2]+
				"\nd: "+letterCountArray[3]+"e: "+letterCountArray[4]+"\nf: "+letterCountArray[5]+"g: "+letterCountArray[6]+
				"\nh: "+letterCountArray[7]+"i: "+letterCountArray[8]+"\nj: "+letterCountArray[9]+"k: "+letterCountArray[10]+
				"\nl: "+letterCountArray[11]);
		JOptionPane.showMessageDialog(null,"m: "+letterCountArray[12]+"\nn: "+letterCountArray[13]+"o: "+letterCountArray[14]+"\np: "
				+letterCountArray[15]+"q: "+letterCountArray[16]+"\nr: "+letterCountArray[17]+"\ns: "+letterCountArray[18]+"\nt: "+
				letterCountArray[19]+"u: "+letterCountArray[20]+"\nv: "+letterCountArray[21]+"w: "+letterCountArray[22]+"\nx: "+
				letterCountArray[23]+"y: "+letterCountArray[24]+"\nz: "+letterCountArray[25]);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	//asks for input and plugges into the printResults method	
	 String	input=JOptionPane.showInputDialog("Welcome please input a sentence!!!");

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


I'm sure you are only beginning the program, right?
If I didn't misunderstood you, your objective is to split an text into sentences (delimited by dots) and then count the number of letters on each sentence. I've looked your code and just realized it got a large number of little mistakes that lead it to malfunction.

Here are some tips:
   1. Never skip thinking! You got to know what you want to do before writing the very first line of code.
   2. Try to split the problem in smaller parts ( in the one you mentioned, "Split the text into sentences" & "Count letters for each sentence" were clearly smaller parts of the main problem)
   3. Keep it simple. The simpler code is always the better (Note that simpler and smaller are different things). Of course there will be codes that are complex by the nature of the problem they solve.

That said, I written the two methods for tokinize the text and count strings.
Anyway I recommend you to try the thing again from the beginning, making sure every single method works fine, before moving to the next one.

Best Regards,
Bruno Vinicius

 * This method will tokenize the text in various sentences. Only '.' is
 * considered as sentence delimiter. A maximum of 10 sentences are allowed.
 * @param text
 *            The text to tokenize.
 * @return An array list of the sentences
public ArrayList<String> sentenceTokenizer(String text) {
    StringBuilder builder = new StringBuilder(text);
    ArrayList<String> list = new ArrayList<String>();
    // tokenizes the text into sentences (only '.' considered)
    while (builder.length() != 0) {
        // asks for the next '.' index
        int idx = builder.indexOf(".");
        // if an '.' wasn't found, consider the remaining text
        // to be the final sentence
        if (idx == -1) {
            idx = builder.length();
        // removes the sentence from the builder
        String sentence = builder.delete(0, idx).toString();
        // adds it into the ArrayList
    return list;
 * Counts how many letters are on a sentence.
 * @param sentence
 *            The sentence to analyze
 * @return The letters count on the given sentence
public int letterCount(String sentence) {
    int counter = 0;
    // scans the string looking if each char is a letter
    for (int i = 0; i < sentence.length(); i++) {
        // gets the next char
        char c = sentence.charAt(i);
        // asks Character class if it is a letter
        if (Character.isLetter(c)) {
            // if so, increment the counter
    return counter;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danielccAuthor Commented:
thanks man...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.