Complete the code to print the first command line argument.
public class Main { public static void main(String[] args) { System.out.println([1]); } }
The command line arguments are stored in the args array. To access the first argument, use args[0].
Complete the code to check if any command line arguments were passed.
public class Main { public static void main(String[] args) { if ([1] > 0) { System.out.println("Arguments received."); } else { System.out.println("No arguments."); } } }
length() with parentheses which causes a compile error.size() or count() which do not exist for arrays.The number of command line arguments is given by args.length. It returns the size of the array.
Fix the error in the code to print all command line arguments separated by spaces.
public class Main { public static void main(String[] args) { for (int i = 0; i < [1]; i++) { System.out.print(args[i] + " "); } } }
length() with parentheses causing errors.size() or count() which are not valid for arrays.The loop should run while i is less than args.length, which is the number of command line arguments.
Fill both blanks to print the last command line argument safely.
public class Main { public static void main(String[] args) { if (args.[1] > 0) { System.out.println(args[[2]]); } else { System.out.println("No arguments."); } } }
length() with parentheses.size or count which are invalid for arrays.To safely print the last command line argument, first check if args.length > 0. Then access args[args.length - 1] for the last element.
Fill all three blanks to print all arguments in uppercase separated by commas.
public class Main { public static void main(String[] args) { for (int [1] = 0; [2] < args.[3]; [1]++) { System.out.print(args[[1]].toUpperCase()); if ([1] < args.length - 1) { System.out.print(", "); } } } }
size instead of length for arrays.The loop variable is commonly i. The condition uses args.length to get the number of arguments.