title: How to count the number of consonants in a String in Java?
body: <p>I am writing a program that counts the number of vowels and consonants from a sentence entered by the user. My code below counts the number of vowels but it give me strange numbers for consonant counts. For example if i enter "g" I get consonant count being 10.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class VowelCount{
    public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a sentence :");
      String sentence = scan.nextLine();
  
      String vowels = "aeiouAEIOU";
      int vowelCount = 0;
      int consCount = 0;
      int i;
  
        for(i = 0; i &lt; sentence.length(); i += 1){
          char currentChar = sentence.charAt(i);
          int index;
          for(index = 0; index &lt; vowels.length(); index += 1){
            if(vowels.charAt(index) == (currentChar)){
              vowelCount++;
            }else if(Character.isLetter(currentChar) &amp;&amp; (vowels.charAt(index) == (currentChar))){
              consCount++;
            }
          }
        }
        System.out.println(consCount);
        System.out.println(vowelCount);
    }   
  }
  </code></pre>