{"id":4516,"date":"2014-03-30T13:11:35","date_gmt":"2014-03-30T13:11:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/improvement-s-to-my-java-generic-console-input-method-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:11:35","modified_gmt":"2014-03-30T13:11:35","slug":"improvement-s-to-my-java-generic-console-input-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/improvement-s-to-my-java-generic-console-input-method-collection-of-common-programming-errors\/","title":{"rendered":"Improvement\/s to my Java generic console input method?-Collection of common programming errors"},"content":{"rendered":"<p>You can use the <code>Class#cast<\/code>method instead, but should leave some comments, because even though <code>cast<\/code> does not create a warning, it can throw a ClassCastException at runtime if the cast is not possible.<\/p>\n<pre><code>public static  T readFromInput(String message, Class c) throws Exception{\n    System.out.println(message);\n    Scanner scanner = new Scanner(System.in);\n    try {\n        if(c == Integer.class)\n            \/\/ the next cast to Integer is safe\n            return c.cast(Integer.valueOf(scanner.nextInt()));\n        if(c == String.class)\n            \/\/ the next cast to String is safe\n            return c.cast(scanner.nextLine());\n        if(c == Double.class)\n            \/\/ the next cast to Double is safe\n            return c.cast(Double.valueOf(scanner.nextDouble()));\n        if(c == Float.class)\n            \/\/ the next cast to Float is safe\n            return c.cast(Float.valueOf(scanner.nextFloat()));\n    } catch (InputMismatchException e) {\n        throw new Exception(e);\n    }\n    return null;\n}\n<\/code><\/pre>\n<p>Note that I&#8217;ve changed the method signature slightly &#8211; it should be <code>Class<\/code> and not <code>Class<\/code> to guarantee, that the <code>Class<\/code> instance is consistent with the type parameter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the Class#castmethod instead, but should leave some comments, because even though cast does not create a warning, it can throw a ClassCastException at runtime if the cast is not possible. public static T readFromInput(String message, Class c) throws Exception{ System.out.println(message); Scanner scanner = new Scanner(System.in); try { if(c == Integer.class) \/\/ the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4516","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4516"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4516\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}