So I'm trying to add a while loop so that the program continues to ask for the user to enter a new string phrase after the previous one is counted until the user enters the phrase "quit". However, for some reason if you enter anything but "quit" it just produces a non-stop loop (never asking the user for a new phrase. What am i doing wrong?

// **********************************************************
//   Count.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 Count
{
  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
		
		int countA, countE, countS, countT;
		String quit;

	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 or type \"quit\" to stop : ");
      phrase = scan.nextLine();

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

      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 ' ': 
				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 ("You entered the phrase: " + phrase);
	System.out.println ();
        System.out.println ("Your phrase has: " + countBlank + " blink spaces");
        System.out.println (countA + " A's");
	System.out.println (countE + " E's");
	System.out.println (countS + " S's");
	System.out.println (countT + " T's");
  
  	}
   }
}

Copy your line 31~32 to below line 82. You forget to keep accepting the value from user after you used the value (phrase).

Edited 5 Years Ago by Taywin: n/a

lol figures it would be a stupid mistake like that. That drove me crazy for 2 hours. Thank you.

This question has already been answered. Start a new discussion instead.