{"id":1944,"date":"2018-05-04T15:16:00","date_gmt":"2018-05-04T15:16:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=1944"},"modified":"2024-08-09T09:10:08","modified_gmt":"2024-08-09T09:10:08","slug":"how-to-fix-java-util-inputmismatchexception","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-fix-java-util-inputmismatchexception\/","title":{"rendered":"How to fix java.util.InputMismatchException"},"content":{"rendered":"<h2>How to fix java.util.InputMismatchException<\/h2>\r\n<p>InputMismatchException is a common exception when we work with Java programs that prompts and take input from the user. As the exception name indicates the input doesn&#8217;t match with the program expectations for the data.<\/p>\r\n<h3>The root cause<\/h3>\r\n<p>We get this error when the user enters the input that mismatches with the expected datatype of the input variable in the program. Let&#8217;s say the program expects a<strong> double<\/strong> value and the user enters a <strong>String<\/strong> value.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1946 size-full\" title=\"InputMismatchException\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error.png\" alt=\"InputMismatchException\" width=\"1734\" height=\"1033\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error.png 1734w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error-300x179.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error-1024x610.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error-768x458.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InputMismatchException-Program-Error-1536x915.png 1536w\" sizes=\"auto, (max-width: 1734px) 100vw, 1734px\" \/><\/p>\r\n<h3>\u00a0<\/h3>\r\n<h3>Stack Trace of InputMismatchException<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Exception in thread \"main\" java.util.InputMismatchException\r\nat java.base\/java.util.Scanner.throwFor(Scanner.java:939)\r\nat java.base\/java.util.Scanner.next(Scanner.java:1594)\r\nat java.base\/java.util.Scanner.nextInt(Scanner.java:2258)\r\nat java.base\/java.util.Scanner.nextInt(Scanner.java:2212)\r\nat Demo.main(Demo.java:16)<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>Scanner class throws this exception.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1945\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException.png\" alt=\"\" width=\"1741\" height=\"1031\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException.png 1741w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException-1024x606.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException-768x455.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/java.util_.InputMismatchException-1536x910.png 1536w\" sizes=\"auto, (max-width: 1741px) 100vw, 1741px\" \/><\/p>\r\n<h3>Screenshot<\/h3>\r\n<p>Fix is to handle the exception and let the user know about the error in the input. In the below example, the code is wrapped with a <strong>try &#8211; catch<\/strong> block. So, if the code throws the exception the catch handler is invoked to notify the user with a user- friendly message.\u00a0<\/p>\r\n<p>If the user enters a valid data for the radius then the catch block would not be executed. This is because the code doesn&#8217;t throw the exception when the input is valid.<\/p>\r\n<h3><br \/>Java Listing<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.InputMismatchException;\r\nimport java.util.Scanner;\r\n\/**************************************************\r\n * Demo.java\r\n * @program   \t: \r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : 1.0\r\n **************************************************\/\r\npublic class Demo {\r\n  \/\/main\r\n  public static void main(String[] args) {\r\n    Scanner keyboard = new Scanner(System.in);\r\n    double radius=0.0;\r\n    double areaCircle=0.0;\r\n\r\n    try {\r\n      System.out.println(\"Enter Radius := \");\r\n      radius = keyboard.nextDouble();\r\n      areaCircle = Math.PI*radius*radius;\r\n      System.out.println(\"Area of Circle = \"+ areaCircle);\r\n      keyboard.close();\r\n    }catch(InputMismatchException ime) {\r\n      System.out.println(\"Please enter valid number for radius\");\r\n    }\r\n  } \/\/ end main\r\n} \/\/ end Demo<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Sample run output<\/strong><\/p>\r\n<p>Enter Radius := <br \/>zzz<br \/>Please enter valid number for radius<\/p>\r\n<p>The string &#8216;zzz&#8217; is invalid data for radius with <strong>double<\/strong> datatype. The code in the try block throws an exception. The catch block handle it and displays a message to the user.\u00a0<\/p>\r\n<p>&#8212;<\/p>\r\n<p>Java Tutorial on this website:<\/p>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\r\n<p><br \/>For more information on Java, visit the official website :<\/p>\r\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>How to fix java.util.InputMismatchException InputMismatchException is a common exception when we work with Java programs that prompts and take input from the user. As the exception name indicates the input doesn&#8217;t match with the program expectations for the data. The root cause We get this error when the user enters the input that mismatches with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1944","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1944"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1944\/revisions"}],"predecessor-version":[{"id":23861,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1944\/revisions\/23861"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}