Checking Command Line Arguments

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

The blanks are filled, below.

Checking Command Line Arguments

Of course, you can use any names for the arguments of the catch{} clauses. The blocks have to be in the given order since FileNotFoundException is a child class of IOException Also, the blocks should actually do something.

try
{
    . . . . 
}
catch(  FileNotFoundException nfx  )
{
}

catch(  IOException iox  )
{
}

All that remains is to check the command line arguments. The command line looks like this:

java copyBytes sourceFile to destinationFile

Here is the part of the program that checks that the command line is correct:

public static void main ( String[] args ) 
{
  DataInputStream  instr;
  DataOutputStream outstr;
  
  if ( _________________ _____ _________________  )
  {
    System.out.println("java CopyBytes source to destination");
    return;
  }
  
  . . .

Sadly, there are three blanks which must be filled. Here is a list of possible blank-fillers:

  • args.length == 3
  • args.length != 3
  • args[1].toUpperCase().equals("TO")
  • !args[1].toUpperCase().equals("TO")
  • &&
  • ||

QUESTION 20:

Fill the blanks. The error message is written out when the command line is incorrect.