Kulas_Code20

Number_Three

Jun 30th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package projects;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NumberThree {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         int vCount = 0;
  9.         int cCount = 0;
  10.         int wCount = 0;
  11.         System.out.println("Enter a sentence: ");
  12.         String sent = sc.nextLine();
  13.  
  14.         sent = sent.toLowerCase();
  15.  
  16.         for (int i = 0; i < sent.length(); i++) {
  17.             // Checks whether a character is a vowel
  18.             if (sent.charAt(i) == 'a' || sent.charAt(i) == 'e' || sent.charAt(i) == 'i' || sent.charAt(i) == 'o'
  19.                     || sent.charAt(i) == 'u') {
  20.                 // Increments the vowel counter
  21.                 vCount++;
  22.             }
  23.             // Checks whether a character is a consonant
  24.             else if (sent.charAt(i) >= 'a' && sent.charAt(i) <= 'z') {
  25.                 // Increments the consonant counter
  26.                 cCount++;
  27.             } else if (Character.isWhitespace(sent.charAt(i))) {
  28.                 wCount++;
  29.             }
  30.         }
  31.         System.out.println("\nNumber of vowels: " + vCount);
  32.         System.out.println("Number of consonants: " + cCount);
  33.         System.out.println("Number of consonants: " + wCount);
  34.  
  35.         System.out.println("\nWould you like to try again? (y/n):");
  36.         char con = sc.next().charAt(0);
  37.         con = Character.toLowerCase(con);
  38.  
  39.         if (con == 'y') {
  40.             main(null);
  41.         } else {
  42.             System.out.println("Exited...");
  43.         }
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment