Java Method definition check if the letter is a vowel or consonant

Question

We would like to write a program that prompts the user to enter a letter.

Check whether the letter is a vowel or consonant.

Here is a sample run: //  w w  w.j ava 2s . c o  m

Enter a letter: B 
B is a consonant 

Enter a letter grade: a 
a is a vowel 

Enter a letter grade: # 
# is an invalid input 
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter: ");
        char ch = input.next().charAt(0);

        if (isVowel(ch)) {
            System.out.println(ch + " is a vowel.");
        } else if (isConsonant(ch)) {
            System.out.println(ch + " is a consonant.");
        } else {//from w  w w .  ja  v  a  2  s .com
            System.out.println("Input error.");
        }
    }

    // checks to see if char is a vowel a e i o u
    // note: not case sensitive
    public static boolean isVowel(char ch) {

        //your code here

    }

    public static boolean isConsonant(char ch) {

        //your code here

    }

}



import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter: ");
        char ch = input.next().charAt(0);

        if (isVowel(ch)) {
            System.out.println(ch + " is a vowel.");
        } else if (isConsonant(ch)) {
            System.out.println(ch + " is a consonant.");
        } else {
            System.out.println("Input error.");
        }
    }

    // checks to see if char is a vowel a e i o u
    // note: not case sensitive
    public static boolean isVowel(char ch) {

        ch = Character.toUpperCase(ch);

        return !(ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' &&ch != 'U');

    }

    public static boolean isConsonant(char ch) {

        ch = Character.toUpperCase(ch);

        return !isVowel(ch) && (ch >= 'A' && ch <= 'Z');

    }

}



PreviousNext

Related