This is my now finished code:

// **********************************************************
// Count.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase and quits upon the user entering the phrase "quit".
// Modified by: Chad Watkins
// **********************************************************
import java.util.*;
import java.lang.*;

public class Count 
{

    public static void main(String[] args) 
	 {
        String phrase; // a string of characters
		  int countBlank; // the number of blanks (spaces) in the phrase
		  int countA;	// The number of As in the phrase.
		  int countE;	// The number of Es in the phrase.
		  int countS;	// The number of Ss in the phrase.
	     int countT;	// The number of Ts in the phrase.
        int length; // the length of the phrase
        char ch; // an individual character in the string
        Scanner scan = new Scanner(System.in);
		  
		  // Print a program header
        System.out.println();
        System.out.println("Character Counter");
        System.out.println();
		  
		  // Read in a string and find its length
        System.out.print("Enter a sentence or phrase, quit to end: ");
        phrase = scan.nextLine();
		  phrase = phrase.toUpperCase();
		  length = phrase.length();
		  
		  // Ends the program if the user enteres quit.
		  while (! phrase.equals("QUIT"))
		  {
		  		// Initialize counts.
       		countBlank = 0;
				countA = 0;
				countE = 0;
				countS = 0;
				countT = 0;
					  
		  		// a for loop to go through the string character by character
	     		// and count the blank spaces
				for (int countLetter = 0; countLetter < length; countLetter = countLetter + 1)
				{
					// Assigns the current character read to ch.
					ch = phrase.charAt(countLetter);
										
					// Compares each character and counts the instances of the letters A, E, S, T, and the number
					// of blank spaces.
					switch (ch)
					{
					case ' ':
						countBlank = countBlank + 1;
						break;
					case 'A':
						countA++;
						break;
					case 'E':
						countE++;
						break;
					case 'S':
						countS++;
						break;
					case 'T':
						countT++;
						break;
					}												
			   }
		  
	 	 	 	// Print the results
       	 	System.out.println();
       	 	System.out.println("Number of blank spaces: " + countBlank);
       	 	System.out.println("Instances of the letter 'A': " + countA);
		  		System.out.println("Instances of the letter 'E': " + countE);
		 	 	System.out.println("Instances of the letter 'S': " + countS);
		  		System.out.println("Instances of the letter 'T': " + countT);
			
			// Reprompts the user to enter a phrase.
			System.out.print("Enter a sentence or phrase, quit to end: ");
        	phrase = scan.nextLine();
			phrase = phrase.toUpperCase();
			length = phrase.length();
			}		  
    }
}
[CODE = Java]

I was having problems getting the correct number of Es, Ss, and Ts. My instructor had a good laugh and told me I was "printing A 4 times!" In other words, the print lines were like this:

System.out.println("Instances of the letter 'A': " + countA);
		  		System.out.println("Instances of the letter 'E': " + countA);
		 	 	System.out.println("Instances of the letter 'S': " + countA);
		  		System.out.println("Instances of the letter 'T': " + countA); 
[CODE = Java]

Here's what I tried to fix it since I just didn't get it:
1) Removing the uppercase conversion and checking for upper and lower case instances of each letter.
2) An if-else block that does the same thing as the switch statement, with and without the conversion.
3) Stacked if statements, just in case it would make a difference.
4) Taking each letter out as a substring, hoping that the case statements would recognize a string better than the characters.
5) Placing the ASCII code numbers in place of the characters.

Anyway, at least I learned a valuable lesson. Next time, I'll look for obvious problems first. :)

Recommended Answers

All 3 Replies

Do you have a question about this code? Or were you just sharing a personal anecdote? Generally, the software development forums are reserved for people to ask questions to try to fix thier code. But yeah, sometimes the most obvious solution is the correct one. In fact that quote was my signature a little while back ;)

My apologies. It's just anecdote. Like my post states at the top, it's finished code. I just thought that some people would get a kick out of someone wasting so much time on such an obvious mistype. :)

My apologies. It's just anecdote. Like my post states at the top, it's finished code. I just thought that some people would get a kick out of someone wasting so much time on such an obvious mistype. :)

No need to apologize. Many programmers face the same problem you have described in this thread. Its very easy to overlook a key ingredient that is missing from a code. Many times a single character can mean the difference between a successful program and a frustrating message from the compiler. ;)

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.