public class InputDemo { public static void main ( String[] args ) { int sum = 0; for (int j=0; j < args.length; j++ ) sum += Integer.parseInt( args[j] ); System.out.println( "Sum: " + sum ); } }
Put quotes around a command line argument if it contains spaces.
C:\>java SomeProgram "one argument" "another argument"
The Java system creates an array of String
references if there are any on the command line.
But the program is free to ignore them.
(Thought Question: ) How can a program tell how many command line arguments it has been given?