{"id":188,"date":"2009-07-27T21:50:38","date_gmt":"2009-07-27T20:50:38","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=188"},"modified":"2009-07-27T21:50:54","modified_gmt":"2009-07-27T20:50:54","slug":"add-two-numbers-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/add-two-numbers-2\/","title":{"rendered":"Add two numbers"},"content":{"rendered":"<p>This tutorial will demonstrate how to read from the input console (console line) to answer the prompts.  The prompts will need to be a integer value to add the two inputs together.<\/p>\n<p>I am using the InputStreamReader to convert the System.in (console) into a stream, which into converted with a BufferedReader to get the whole line and not just one character\/number.<\/p>\n<p>The code <\/p>\n<pre lang=\"java\">\r\n\/\/ just import the BufferedReader and inputstream reader\r\nimport java.io.BufferedReader;\r\nimport java.io.InputStreamReader;\r\n\r\nclass addtwonumbers\r\n{\r\n       public static void main(String[] args)\r\n       {\r\n              \/\/ system.in reader (e.g. the input from the console)\r\n              InputStreamReader ISR = new InputStreamReader(System.in);       \r\n              \/\/ buffer the console reader\r\n              BufferedReader BR = new BufferedReader(ISR);                     \r\n              \r\n              \/\/ the default values for the two numbers\r\n              int val1 = 0;\r\n              int val2 = 0;\r\n              try \r\n              {\r\n                     \/\/ output the question.\r\n                     System.out.print(\"Enter first number : \");\r\n                     \/\/ read in the console intput one line (BR.readLine) and then convert to a integer\r\n                     val1 = Integer.parseInt(BR.readLine());\r\n                     System.out.print(\"Enter second number : \");\r\n                     val2 = Integer.parseInt(BR.readLine());\r\n              }\r\n              catch (Exception ex)\r\n              {\r\n                     \/\/ if the input was a string.\r\n                     System.out.println(ex.toString());\r\n              }\r\n              \/\/ output the answer of adding both of the values together\r\n              System.out.println(\"Answer = \" + (val1 + val2));\r\n       }\r\n}\r\n<\/pre>\n<p>save that as addtwonumbers.java, because of course java is very picky about the class name to the java file, because Java creates a class of the same name as the class which in-turn is what the java virtual machine uses to execute.<\/p>\n<p>Once compiled (javac addtwonumbers.java) and executed (java addtwonumbers) the output will be<\/p>\n<pre class=\"consoleoutput\">\r\nEnter first number : 30\r\nEnter second number : 23\r\nAnswer = 53\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together. I am using the InputStreamReader to convert the System.in (console) into a stream, which into converted with a BufferedReader to get the whole &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/add-two-numbers-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Add two numbers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[401,398,400],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}