{"id":461,"date":"2020-11-04T03:17:00","date_gmt":"2020-11-04T08:17:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=461"},"modified":"2021-01-01T00:23:04","modified_gmt":"2021-01-01T05:23:04","slug":"java-how-to-pass-command-line-arguments","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-how-to-pass-command-line-arguments\/","title":{"rendered":"Java: How to pass command line arguments"},"content":{"rendered":"\n<p>Following simple code shows how to pass command line arguments to to Java program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CommandLineInput {\n  public static void main (String&#91;] args) {\n    for (String argument: args) {\n       System.out.println(argument);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Running the program<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>javac CommandLineInput.java\njava CommandLine 30000 students<\/code><\/pre>\n\n\n\n<p><strong>Output of this program<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>30000\nstudents<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>&nbsp;All command line arguments are passed to a String array named args defined on line 2 of the code. Since all arguments are stored in an array in the order they were passed, you simply need to access the array to get the command line parameters and cast them to another type in necessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Taking input from command line interactively<\/h3>\n\n\n\n<p>In the previous example, we took command line parameter as input. In this example, the program would prompt users for input and then it would process their input. The following programs simply divides two numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\n\npublic class Division {\n  public static void main(String&#91;] args) {\n    int divident = 0;\n    int divisor = 0;\n    double quotient = 0;\n\n    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n    System.out.println(\"Divident (integer): \");\n    try {\n      divident = Integer.valueOf(br.readLine());\n    } catch (IOException ex) {\n      ex.printStackTrace();\n    }\n    System.out.println(\"Divisor (integer): \");\n    try {\n      divisor = Integer.valueOf(br.readLine());\n    } catch (IOException ex) {\n      ex.printStackTrace();\n    }\n    quotient = divident \/ divisor;\n    System.out.println(\"Quotient: \" + quotient);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>&nbsp;In the 11th line, input is reader by InputStreamReader and stored in BufferedReader. Both values are casted to integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following simple code shows how to pass command line arguments to to Java program: Running the program Output of this program Explanation&nbsp;All command line arguments are passed to a String array named args defined on line 2 of the code. Since all arguments are stored in an array in the order they were passed, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":492,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=461"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/461\/revisions\/462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/492"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}