pTutorial

New way to learn Programming

Java Input Command Line Arguments

Following java program get single input at running time in the form of string that is also known as command line argument.

For example if you entered 38, its not a integer it’s a string in case of command line argument. You cannot use this argument as integer until convert it.

Java Command Line Arguments


public class StringCommandLineArguments {
    
    public static void main(String[] s) {
        
        String str = s[0];
        
        System.out.print("Your Command line Argument is: ");
        
        System.out.print(str);
   
}   
}

Compile:- javac StringCommandLineArguments.java  
Run:- java StringCommandLineArguments ptutorial  

Output

Your Command line Argument is: ptutorial

Java Multi Input from Command Line Arguments


public class StringCommandLineArguments1 {
    
    public static void main(String[] s) {
	
		System.out.println("Multiple input through command line :");
        
       for(int i=0;i<s.length;i++)  {
           
           System.out.println(s[i]);
           
       } 
}
    
}

Compile:- javac StringCommandLineArguments1.java  
Run:- java StringCommandLineArguments1 ptutorial Java  

Output

Multiple input through command line :
ptutorial 
Java

See Also


QUOTES OF THE DAY

We live in this world when we love it.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER