On this page

Related

The problem

Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant.

Breaking it down

Once the user has inputted a character, we will convert the String into a Instream by calling value.chars(). Next using a IntPredicate that checks if character is a vowel we check if any of the values equal a vowel by using Stream.anyMatch(). Something to note, you may consider scrubbing the input value as a user could enter any number of chars, vowels or consonant and anyMatch would return true.

publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.print("Enter a letter: ");Stringvalue=input.next();input.close();booleanisVowel=value.chars().anyMatch(vowel);if(isVowel){System.out.println(value+" is a vowel.");}else{System.out.println(value+" is a consonant.");}}staticIntPredicatevowel=newIntPredicate(){@Overridepublicbooleantest(intt){returnt=='a'||t=='e'||t=='i'||t=='o'||t=='u'||t=='A'||t=='E'||t=='I'||t=='O'||t=='U';}};