I need help creating a while loop the question asks It would
*I need help creating a while loop...
the question asks \"It would be nice to have the program let the user keep entering phrases rather than having to restart it every time. To do this you need another loop surrounding the current code. Add an outer while loop that will continue to execute as long as the user does NOT enter the phrase quit. Modify the prompt to tell the user to enter a phrase or quit to quit. Be careful what should be put inside the while loop and what should not. All declarations should be outside. \"
Here is my code (the bolded part is where I tried creating a while loop but it didn\'t work out);
// ************************************************************
// Count.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase.
// ************************************************************
// We are going to be importing the Scanner class which provides convenient methods for reading input values.
import java.util.Scanner;
public class Count
{
// The following code allows the program to print the written strings that are within the program.
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
char ch = \' \'; // an individual character in the string
// We have to put the following line of code because this allows the program to read the following keyboard input.
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: \");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
int countA = 0; // You have to initialize the letters a, e, s, and t
int countE = 0; // to satisfy question 3 of the Programming Project.
int countS = 0;
int countT = 0;
// a for loop to go through the string character by character
// and count the blank spaces
while (length !=0)
{
while (length != 0)
{
System.out.print(\"Program over.\");
length = scan.nextInt();
}
for (int i = 0; i < length ; i++){
ch = phrase.charAt(i);
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;
case\' \':
countBlank++;
break;
}
}
// Print the results
System.out.println (\" \ Number of blank spaces: \" + countBlank);
System.out.println (\" \ Number of A letters: \" + countA);
System.out.println (\" \ Number of E letters: \" + countE);
System.out.println (\" \ Number of S letters: \" + countS);
System.out.println (\" \ Number of T letters: \" + countT);
}
}
}
Thank you!
Solution
Hi,
I have modified the code and highlighted the code changes below.
Count.java
import java.util.Scanner;
public class Count
{
// The following code allows the program to print the written strings that are within the program.
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
char ch = \' \'; // an individual character in the string
// We have to put the following line of code because this allows the program to read the following keyboard input.
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println (\"Character Counter\");
System.out.println ();
int countA = 0; // You have to initialize the letters a, e, s, and t
int countE = 0; // to satisfy question 3 of the Programming Project.
int countS = 0;
int countT = 0;
while(true){
// Read in a string and find its length
System.out.print (\"Enter a sentence or phrase (or \'quit\' to stop): \");
phrase = scan.nextLine();
if(phrase.equalsIgnoreCase(\"quit\")){
break;
}
length = phrase.length();
// 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 i = 0; i < length ; i++){
ch = phrase.charAt(i);
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;
case\' \':
countBlank++;
break;
}
}
// Print the results
System.out.println (\" \ Number of blank spaces: \" + countBlank);
System.out.println (\" \ Number of A letters: \" + countA);
System.out.println (\" \ Number of E letters: \" + countE);
System.out.println (\" \ Number of S letters: \" + countS);
System.out.println (\" \ Number of T letters: \" + countT);
}
}
}
Output:
Character Counter
Enter a sentence or phrase (or \'quit\' to stop): Hi, good morning
Number of blank spaces: 2
Number of A letters: 0
Number of E letters: 0
Number of S letters: 0
Number of T letters: 0
Enter a sentence or phrase (or \'quit\' to stop): Hi good evening
Number of blank spaces: 2
Number of A letters: 0
Number of E letters: 2
Number of S letters: 0
Number of T letters: 0
Enter a sentence or phrase (or \'quit\' to stop): quit




