0

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
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Learning_Java
  • 65
  • 1
  • 4
  • 12
  • 2
    "*I already tried `while (phrase != "quit")`*" start again, but before you write anything read this [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Oct 17 '16 at 23:29
  • When you say `while (phrase != "quit")` "did not work", in what way did it not work? Did it just not respond properly to "quit", or was something else wrong too? – Douglas Oct 17 '16 at 23:32
  • Might help. http://stackoverflow.com/questions/26367626/user-input-to-repeat-program-in-java – OneCricketeer Oct 17 '16 at 23:33
  • I changed 'while (phrase != "quit")' to ' while (!phrase.equals("quit"))' when I used '!=', none of the print statements printed and when i used '.equals', the for loop was still executed when I typed "quit". What can I do to make it quit when I type "quit"? I also don't really agree that this post is a duplicate because my original question was focusing on how to quit the code using while loop. Thank you very much for your help too. – Learning_Java Oct 18 '16 at 00:03
  • to specify, I need a while loop that will continue to execute as long as the user does NOT enter the phrase 'quit' – Learning_Java Oct 18 '16 at 00:31

0 Answers0