0

So my program won't stop when the user inputs quit and also the variables keep adding up each time it's ran, when I only one them to count the characters in one run, then count the next input. So the program counts the specific characters in a string but I want it to count no matter what the first time and after the first time I want the user to have the option to say stop and end the program

public class Count {
    public static void main (String[] args) {
        String phrase;    // a string of characters
        int blankCount;   // the number of blanks (spaces) in the phrase 
        int length;       // the length of the phrase
        char ch;          // an individual character in the string

        Scanner scanner = new Scanner(System.in);

        System.out.println ();
        System.out.println ("Character Counter");
        System.out.println ();
        System.out.print ("Enter a sentence or phrase: ");

        blankCount = 0;
        int aCount = 0;
        int eCount = 0;
        int sCount = 0;
        int tCount = 0;
        int timesRan = 0;   

        do {    
            phrase = scanner.nextLine();
            length = phrase.length();

            for(int i=0; i<length; i++){
                switch(phrase.charAt(i)){
                case ' ':
                    blankCount++;
                    break;
                case 'a':
                    aCount++;
                    break;
                case 'A':
                    aCount++;
                    break;
                case 'e':
                    eCount++;
                    break;
                case 'E':
                    eCount++;
                    break;
                case 's':
                    sCount++;
                    break;
                case 'S':
                    sCount++;
                    break;
                case 't':
                    tCount++;
                    break;
                case 'T':
                    tCount++;
                    break;
                }
                timesRan++;      
            }
            System.out.println ("Number of space characters: " + blankCount);
            System.out.println ("Number of a's: " + aCount);
            System.out.println ("Number of e's: " + eCount);
            System.out.println ("Number of s's: " + sCount);
            System.out.println ("Number of t's: " + tCount);

            System.out.print("Enter a phrase or enter quit, to quit: ");
        } while(phrase != "quit" && timesRan >= 0);
    }    
}
OliPro007
  • 415
  • 6
  • 17
Justin
  • 27
  • 6
  • 2
    use `while (!phrase.equals("quit") && timesRan >= 0);` instead of `!=` operator. – WrongRhyme Feb 21 '16 at 22:20
  • 1
    you shouldn't compare strings using `==`, and the rest of your question(s?) I don't understand. – John3136 Feb 21 '16 at 22:21
  • Hey thank you guys that solved my main problem. Sorry for explaining poorly, but the other issue is, for examples say I run my program twice and input 's', the first time it will count that I have 1 's'. Then second time it will count have have 2. I want it to only count the input without the additional first several inputs. So I want to enter 's' and every time have it tell we that there is one 's'. – Justin Feb 21 '16 at 22:45

1 Answers1

2

Try adding the default case in switch statement. Also instead of comparing with !=, try .equals method in the while. So that code in the while part should be :while (phrase.equals("quit")==false && timesRan >= 0)

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61