Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projects;
- import java.util.Scanner;
- public class NumberThree {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int vCount = 0;
- int cCount = 0;
- int wCount = 0;
- System.out.println("Enter a sentence: ");
- String sent = sc.nextLine();
- sent = sent.toLowerCase();
- for (int i = 0; i < sent.length(); i++) {
- // Checks whether a character is a vowel
- if (sent.charAt(i) == 'a' || sent.charAt(i) == 'e' || sent.charAt(i) == 'i' || sent.charAt(i) == 'o'
- || sent.charAt(i) == 'u') {
- // Increments the vowel counter
- vCount++;
- }
- // Checks whether a character is a consonant
- else if (sent.charAt(i) >= 'a' && sent.charAt(i) <= 'z') {
- // Increments the consonant counter
- cCount++;
- } else if (Character.isWhitespace(sent.charAt(i))) {
- wCount++;
- }
- }
- System.out.println("\nNumber of vowels: " + vCount);
- System.out.println("Number of consonants: " + cCount);
- System.out.println("Number of consonants: " + wCount);
- System.out.println("\nWould you like to try again? (y/n):");
- char con = sc.next().charAt(0);
- con = Character.toLowerCase(con);
- if (con == 'y') {
- main(null);
- } else {
- System.out.println("Exited...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment