Right now my code runs just the way I want it to. I would like the
Enter a sentence or phrase
part to continue until the user enters "quit". I would like to use a while statement. Every way I have tried to enter phrase not equal to "quit" has not worked. Do I need to declare "quit" somewhere or what? Any help would be greatly appreciated.
// **********************************************************
// length.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase.
// **********************************************************
import java.util.Scanner;
public class Length
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
int countA = 0; // the number of a's in the phrase
int countE = 0; // the number of e's in the phrase
int countS = 0; // the number of s's in the phrase
int countT = 0; // the number of t's in 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: ");
//System.out.print ("Enter a sentence, phrase, or 'quit' to exit program: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
// and count the blank spaces
for(int i = 0; i < phrase.length(); i++)
{
ch = phrase.charAt(i);
switch (ch)
{
case ' ': countBlank++;
break;
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
}
}
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ("Number of A's: " + countA);
System.out.println ("Number of E's: " + countE);
System.out.println ("Number of S's: " + countS);
System.out.println ("Number of T's: " + countT);
System.out.println ();
}
}