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 ();
    }
}

Wrap the thing from that point on in a while (!phrase.equals("quit")) { loop.

Where would I place this? while (!phrase.equals("quit")) { If I do it before

length = phrase.length();

it won't run because I haven't initialized "phrase". If I place it after the

length = phrase.length();

, one of two things happens depending on where I place my "}". Option one at the end is it causes an infinite loop or option two if I place the "}" sooner is it does nothing.

Where would I place this? while (!phrase.equals("quit")) { If I do it before

length = phrase.length();

it won't run because I haven't initialized "phrase".

Well, think about that a bit.

...
        String phrase; // a string of characters
        ...
        // Read in a string and find its length
        System.out.print ("Enter a sentence or phrase: ");
        ...
        System.out.println ();
    }
...
        String phrase = ""; // a string of characters
        ...
        while (!phrase.equals("quit")) {
            // Read in a string and find its length
            System.out.print ("Enter a sentence or phrase: ");
            ...
            System.out.println ();
        }
    }

Edited 7 Years Ago by masijade: n/a

I really don't understand what you are saying. How would I initialize "phrase" if i put

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

so soon? I'm really new with java so please be kind to me.

Take a close look at my previous post.

The first code block is your code, the second code block is modifications to that code.

I really don't understand what you are saying. How would I initialize "phrase" if i put

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

so soon? I'm really new with java so please be kind to me.

Phrase is initialized in the code that you posted. It is initialized to "", which is the empty String. In order to quit using the code Masijade showed you, after you say "please enter a sentence or phrase" inside of the while loop, you would need to read in the user's input, then store it into the phrase variable. If that doesn't make sense, then read about the Scanner class. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Relevant topics: Where it says,

For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

But you cannot read in Strings with the nextInt method, so read about the nextLine() method.

Thx a lot masijade. I finally got it ;). Can you please explain why that works? I would like to learn. I would have never of figured that out on my own. My knowledge of java is fairly limited.

Edited 7 Years Ago by COKEDUDE: n/a

Because phrase is a String object, and the "equals" method for the String class is implemented so that it compares two Strings to see if they are the same. If you are interested, you should read about the following topics:

Method overriding
Inheritance

And also, mark solved threads as solved

Edited 7 Years Ago by BestJewSinceJC: n/a

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