Accepting dynamic input to the Java program
In Java program we accept the data dynamic we have 4 approaches.
1) through command prompt2) through keyboard
3) through properties file (or) resources bundle file.
4) through XML document file
LENGTH
Length is one of the "Implicit variable" created during at the compile time by the "Java environment" and it is available to the entire Java program for two purpose. They are:
- It finds number of elements present in an "Array" [determine the size of an array like [5] ]
- Internally JVM will treat "All Fundamental Arrays" .
Syntax :- Array name.length
(Here Array name represents name of the array variables).
Example:-1 int a [4] = { 10,20,30,40};
System.out.println ( " Number of elements ="+ a. length ) ;// 4
for ( int i=0 ; i< a.length ; i++ )
{
System.out.println (a [ i ]);
}
Example:-2 float x [2] = { 10.1,20.1 };
System.out.println ( " Number of elements = "+ x.length);//2
for ( int i=0; i< x.length ; i++)
{
System.out.println( x [ i ] );
}
Example:-3 String s [ ] = { "C++" , " Java ", " Oracle" };
System.out.println ( "Number of elements = " + s.length ) ; //3
for ( int i=0; i< s.length; i++)
{
System.out.println( s[ i ] );
}
Consider the following statements:
Accepting the data from dynamic form in the below following small example.
EX:- D://Java > Java MulDemo 10 20
Command prompt Java program values
The values passing through the Java program from command prompt " Command Line Argument " .
When we pass (or) enter the "command line argument" internally the following steps will be executed and makes to an their makes to an understand arguments passing to which method, residing in which method and how to process.
- Class Loader Subsystem loads "MulDemo" along with command line arguments [ 10,20 ].
- JVM takes loaded class first as "MulDemo" along with command line argument [ 10,20 ] and accounted then ( EX:- length L )
- JVM looks for main ( String k [ 2 ] ) [ 10,20 ];.
- JVM calls the main ( String k [ 2 ] ) [ 10,20 ]; as "MulDemo" as main(string k [ 2 ] ) [10,20];
- Hence all the command line argument is taken by the JVM and it passing into the "main(String k[] )" and available in the form of "Array of Object of String class". ( EX:- String k( [ ] )
NOTE :- The reason for taking array of object of string class is that to accept command line arguments.
Q. write a java program which will accept the dynamic input from command line arguments through the command prompt.
Program
class CommandLineArguements
{
public static void main(Strings k[])
{
System.out.println("Command Line Arguemants");
for (int i=0; i<=k.length;i++)
{
System.out.println(k[i]);
}
}
Q. write a java program which will accept the dynamic input from command line arguments through the command prompt.
Program
class CommandLineArguements
{
public static void main(Strings k[])
{
System.out.println("Command Line Arguemants");
for (int i=0; i<=k.length;i++)
{
System.out.println(k[i]);
}
}
Q Write a Java program which will accept the command line arguments .
Program :-
class CmdLine
{
public static void main(String l[])
{
System.out.println("Number of cmd line args ="+l.length);
System.out.println("Command Line Arguments");
System.out.println("========================");
for (int i=0; i<l.length; i++) //Here For Loop Condition start.
{
System.out.println( l [i] );
}
System.out.println("=========================");
} // main()
} //cmdline class
Q. Write a Java program which will accept the data & shown student information from the command line arguments .
Program :-
class StudentInfo
{
public static void main(String k[])
{
System.out.println("Number of cmd line args ="+k.length);
System.out.println("Command Line Arguments");
System.out.println("========================");
for (int i=0; i<k.length; i++) //Here For Loop Condition start.
{
System.out.println( k [i] );
}
System.out.println("=========================");
} // main()