-1

for school I have to make a scanner that will be able to take a string input and count the amount of vowels like 1 "a" etc. my problem is that when I enter a string it will not go through the loops and I can just continuously type and enter does nothing. (I am using BlueJ to code by the way). Thanks for any help!

package VowelCounter;

/**
 * Description:
 * Author: Jack Cannon
 * Date: 11/15/16
 */

import java.util.Scanner;
public class VowelCounter
{
public static void main(String [] args)
{
     Scanner input = new Scanner(System.in);
     //Prompt the user to enter a phrase 
     System.out.println("Type in a sentence.");
     String phrase = input.nextLine();
     //as long as the string isn't "quit", 
     //count the vowels in the string.
     int length = phrase.length();
     while(phrase != "quit")
    {
         int a = 0;
         int e = 0;
         int i = 0;
         int o = 0;
         int u = 0;  
       for(int c = 0; length < 1; length++)
       {  
           char vowels = phrase.charAt(c);
         if(vowels == 'a')
         {
         }
         else if(vowels == 'e')
         {
         }
         else if(vowels == 'i')
         {
         }
         else if(vowels == 'o')
         {
         }
         else if(vowels == 'u')
         {
         }
       }
   }    
   System.out.println("There are " + 'a' + "a's");
   System.out.println("There are " + 'e' + "e's");
   System.out.println("There are " + 'i' + "i's");
   System.out.println("There are " + 'o' + "o's");
   System.out.println("There are " + 'u' + "u's");
   //print out the count of the vowels for this string.      
   //prompt the user for another phrase.
   }
  }
John3136
  • 28,809
  • 4
  • 51
  • 69
Jack
  • 1

2 Answers2

1

There are a few things wrong with the code that you shared above.

First of all, your while loop has a condition of phrase != "quit". This is wrong for two reasons. The first being that this is not how you compare strings in java. For that, see this. The second reason is that you are not ever changing the variable phrase within your while loop, so this will result in an infinite loop because phrase will never be "quit" unless that is what the user types in. If you only need input from the user once, I do not see a need for this while loop.

The second reason that your code is wrong is because the if statements within your for loop (to see what character the current char is) are doing nothing. They are just empty blocks of code at this point. Most likely what you need to do is increment that appropriate vowel counter.

Finally, you are using an incorrect string concatenation method in your print lines. The correct way to concatinate in Java is the following:

"There are " + a + "a's" // a = 1
=> "There are 1 a's"

The use of the single quotes is incorrect; you would use that if you wanted to print the letter 'a', like so:

"There are a a's"
Community
  • 1
  • 1
Saketram Durbha
  • 450
  • 3
  • 14
0

This should fix your problem, I only added 2 if statements in the loop, you can add the rest.

public static void main(String [] args){
    Scanner input = new Scanner(System.in);
    //Prompt the user to enter a phrase 
    System.out.println("Type in a sentence.");
    String phrase = input.nextLine();
    //as long as the string isn't "quit", 
    //count the vowels in the string.
    int length = phrase.length();
    int a = 0;
    int e = 0;
    int i = 0;
    int o = 0;
    int u = 0;  
    while(!phrase.equals("quit")){
        for(int c = 0; c<length; c++){  
            char vowels = phrase.charAt(c);
            if(vowels == 'e'){
                e++;
            }
            else if(vowels == "o"){
                o++;
            }
        }
        System.out.println("There are " + a + "a's");
        System.out.println("There are " + e + "e's");
        System.out.println("There are " + i + "i's");
        System.out.println("There are " + o + "o's");
        System.out.println("There are " + u + "u's");
        System.out.println("enter another sentence");
        phrase = input.nextLine();
    }    
}
darkhouse
  • 205
  • 5
  • 15