Paraknight Paraknight - 1 month ago 4
Java Question

How do I create a while loop around a for loop that will allow the user to quit the loop?

I am new to loops and I coded a for loop already that prompts the user to input a phrase and the for loop prints out how many spaces, a's, e's ,s's, and t's there are. I need to make a while loop that will allow the user to type in "quit" to quit the loop. I am also new to learning switch to print out the letters. I have already tried experimenting with my code to fix my errors (either an ongoing loop or no results) for over a week. Here is my code below and thank you in advance.

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;

//I am not sure how to set up the start of the while loop
//while (!phrase.equals("quit")){
//while (phrase != "quit"){

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

Number of blank spaces: 1

Number of a's: 2
Number of e's: 2
Number of s's: 2
Number of t's: 2

Answer

You are on the right track, but you do need to read the phrase again once the process is completed to make the while loop work. Otherwise, it will be endless loop. Here are few changes I made :

 while (!phrase.equals("quit")){

        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);
        phrase =scan.nextLine(); //read next phrase from user to continue.
        }