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. :)