{"id":11654,"date":"2020-10-09T11:17:46","date_gmt":"2020-10-09T17:17:46","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=11654"},"modified":"2020-11-16T19:15:40","modified_gmt":"2020-11-17T02:15:40","slug":"check-vowel-or-consonant-in-java","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/check-vowel-or-consonant-in-java","title":{"rendered":"Check vowel or consonant in Java"},"content":{"rendered":"<p><strong>Question:<\/strong> Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant. Must display any input with non-letter characters. This is based on a question from Java textbook by Dr. Daniel Liang.<\/p>\n<p><!--more--><br \/>\n<div class=\"alert alert-warning\" role=\"alert\"><p class=\"printonly\"><strong>Warning!<\/strong><\/p> Please import selected Java packages or use <em>import java.util.*;<\/em>. Do not copy my code as per copyright and plagiarism regulations. This Java code is provided as it is for demonstration purposes.<\/div><\/p>\n<p><strong>Solution 1:<\/strong> The following code checks if the user input only contains one letter and display a message when there are more than one letter.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class VolConst { \r\n public static void main(String&#x5B;] args) {\r\n  Scanner input = new Scanner(System.in);\r\n  System.out.print(&quot;Enter a letter: &quot;);\r\n  String s = input.nextLine(); \r\n  if (s.length() != 1) {\r\n   System.out.println(&quot;You must enter exactly one character&quot;);\r\n   return;\r\n  } \r\n  char ch = Character.toUpperCase(s.charAt(0));\r\n  if (ch &gt; 'Z' || ch &lt; 'A') {\r\n   System.out.println(s + &quot; is an invalid input&quot;);\r\n   return;\r\n  } \r\n  if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {\r\n   System.out.println(s + &quot; is a vowel&quot;);\r\n   return;\r\n  }\r\n  System.out.println(s + &quot; is a consonant&quot;); \r\n } \r\n}\r\n<\/pre>\n<p><strong>Solution 2:<\/strong> The following code accomplish the same task but does not verify if the user only input one letter. When a user enter more than one letter, the program takes the first letter in memory.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class VolConst {\r\n  public static void main(String&#x5B;] args) {\r\n    Scanner input = new Scanner(System.in);\r\n    System.out.print(&quot;Enter a letter: &quot;);\r\n    char letter = input.nextLine().charAt(0);\r\n    \r\n    if (Character.toUpperCase(letter) == 'A' || Character.toUpperCase(letter) == 'E' || Character.toUpperCase(letter) == 'I' \r\n        || Character.toUpperCase(letter) == 'O' || Character.toUpperCase(letter) == 'U')\r\n      System.out.println(letter + &quot; is a vowel&quot;);\r\n    else if (Character.isLetter(letter))\r\n      System.out.println(letter + &quot; is a consonant&quot;);\r\n    else\r\n      System.out.println(letter + &quot; is an invalid input&quot;);\r\n  }\r\n}\r\n<\/pre>\n<p>Note the <em>Character.isLetter()<\/em> function can be used to check if the user entered anything other than letters.<\/p>\n<p><b>Output:<\/b><br \/>\n<u>Sample Run 1<\/u><br \/>\nEnter a letter: B<br \/>\nB is a consonant<\/p>\n<p><u>\ufeffSample Run 2<\/u><br \/>\nEnter a letter grade: a<br \/>\na is a vowel<\/p>\n<p><u>\ufeffSample Run 3<\/u><br \/>\nEnter a letter grade: #<br \/>\n# is an invalid input<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant. Must display any input with non-letter characters. This is based on a question from Java textbook by Dr. Daniel Liang.<\/p>\n","protected":false},"author":2,"featured_media":11646,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[48],"class_list":["post-11654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","tag-java"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2020\/10\/java_code_tictactoe.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=11654"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11654\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/11646"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=11654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=11654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=11654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}