{"id":103,"date":"2025-02-21T09:43:12","date_gmt":"2025-02-21T09:43:12","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=103"},"modified":"2025-02-21T09:43:12","modified_gmt":"2025-02-21T09:43:12","slug":"program-for-use-of-command-line-arguments","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2025\/02\/21\/program-for-use-of-command-line-arguments\/","title":{"rendered":"Program for use of command line arguments"},"content":{"rendered":"\n<p>Write a program to illustrate the use of command line arguments. Also implement<br>necessary exception handling.<\/p>\n\n\n\n<p><strong>Command-Line Arguments<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>args<\/code> array stores command-line arguments passed when the program is run.<\/li>\n\n\n\n<li><code>args.length<\/code> gives the number of arguments passed.<\/li>\n<\/ul>\n\n\n\n<p><strong>Exception Handling<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NumberFormatException<\/code>: Catches cases where the argument cannot be parsed to an integer.<\/li>\n\n\n\n<li><code>ArrayIndexOutOfBoundsException<\/code>: Catches cases where an expected argument doesn&#8217;t exist (e.g., trying to access <code>args[0]<\/code> when no argument is passed).<\/li>\n\n\n\n<li>A generic <code>Exception<\/code> is used to catch any unexpected errors.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<p>public class CommandLineExample {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    \/\/ Checking if command-line arguments are provided\n    if (args.length == 0) {\n        System.out.println(\"No command-line arguments provided.\");\n    } else {\n        System.out.println(\"Command-line arguments:\");\n\n        \/\/ Loop through the arguments and print each one\n        for (int i = 0; i &lt; args.length; i++) {\n            System.out.println(\"Argument \" + (i + 1) + \": \" + args&#91;i]);\n        }\n    }\n\n    \/\/ Demonstrating exception handling\n    try {\n        \/\/ Example: Try to convert the first argument to an integer\n        if (args.length &gt; 0) {\n            int number = Integer.parseInt(args&#91;0]);  \/\/ Parsing the first argument\n            System.out.println(\"The integer value of the first argument is: \" + number);\n        }\n    } catch (NumberFormatException e) {\n        System.out.println(\"Error: The first argument is not a valid integer.\");\n    } catch (ArrayIndexOutOfBoundsException e) {\n        System.out.println(\"Error: No arguments provided for conversion.\");\n    } catch (Exception e) {\n        System.out.println(\"Unexpected error: \" + e.getMessage());\n    }\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>To Run :<\/p>\n\n\n\n<p><code>javac CommandLineExample.java<\/code><\/p>\n\n\n\n<p><code>java CommandLineExample<\/code><\/p>\n\n\n\n<p><code>java CommandLineExample 10<\/code><\/p>\n\n\n\n<p><code>java CommandLineExample abc<\/code><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program to illustrate the use of command line arguments. Also implementnecessary exception handling. Command-Line Arguments: Exception Handling: Example : public class CommandLineExample { } To Run : javac CommandLineExample.java java CommandLineExample java CommandLineExample 10 java CommandLineExample abc<\/p>\n","protected":false},"author":1,"featured_media":97,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,28],"tags":[31,29,30],"class_list":["post-103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mca","category-object-oriented-programming-in-java","tag-mca","tag-mca-paper-solution","tag-object-oriented-programming-in-java"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions\/104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/97"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}