14 How to load data dynamically from keyword using Command Line Argument in Java?

Ways to Load Data Dynamically: 
1) Command Line Argument
2) java.io.BufferedReader
3) java.util.Scanner
4) java.io.DataInputStream
5) java.io.Console
6) System.getProperty()
7) swing/awt/applet


How to Read Data from Command Line Argument

Command Line Argument: Meanwhile of writing program execution syntax. If you are sending any value those values we can called as Command Line Argument.

Code:  Suppose we have one java file CLA.java

CLA.java

class CLA{
public static void main(String[] s){
System.out.println("Command Line Argument Program");

  String s1 = s[0];
String s2 = s[1];
String s3 = s[2];

System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}

}

Output: 

If we not sending any value then we get one error : java.lang.ArrayIndexOutofBoundsException


CLA.java   In this if we want to perform addition then we first need to convert it into integer.

class CLA{
public static void main(String[] s){
System.out.println("Command Line Argument Program");

  String s1 = s[0];
String s2 = s[1];
String s3 = s[2];

int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=Integer.parseInt(s3);

System.out.println(a);
System.out.println(b);
System.out.println(c);

System.out.println(a+b+c);
}

}

Output: 

If we not sending any value as integer then we get one error :java.lang.NumberFormatException

Drawbacks of Command Line Argument:

1) We are not getting proper exception information related to Boolean type.
2) We need to recognize number of values
3 )Recognize the type of values
4) We need to write conversion logic each time.

Previous
Next Post »