I am somewhat new to loops and I need to insert a while loop outside of a for loop that counts the spaces, a's, e's, s's, and t's. Once the user types in a phrase, the for loop would lastly print results of the amount of spaces, a's, e's, s's, and t's in the phrase. I want to make a while loop that would allow you to quit the loop if you type in "quit" and will allow you to type in a new phrase that will end up with more results of spaces, a's, e's, s's, and t's. I already tried while (phrase != "quit")
and it did not work. Sorry if there is any confusion but you will probably understand when you see the input and output. Here is the code:
import java.util.Scanner
public class Count
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int i = 0;
char ch; // an individual character in the string
String quit = "quit";
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
System.out.print ("Enter a sentence or phrase (quit to quit): ");
phrase = scan.nextLine(); // read input Scanner
int length = phrase.length(); // the length of the phrase
countBlank = 0;
int countA = 0;
int countE = 0;
int countS = 0;
int countT = 0;
for (i = 0; i < length; i++){
ch = phrase.charAt(i);
if (ch == (' '))
countBlank++;
switch (ch)
{
case 'a': case 'A': countA++; break;
case 'e': case 'E': countE++; break;
case 's': case 'S': countS++; break;
case 't': case 'T': countT++; break;
}
}
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ();
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);
}
}
Output:
Character Counter
Enter a sentence or phrase (quit to quit): aest Aest aest
Number of blank spaces: 2
Number of a's: 3
Number of e's: 3
Number of s's: 3
Number of t's: 3