1. Technology

Parsing Command-Line Arguments Question

By

Level: Beginner

Focus: Command Line Arguments, Converting Strings to Numbers, For each loop, Error handling

Parsing Command-Line Arguments Question

Some applications accept command-line arguments as a way of configuring the application as it is being run. This exercise focuses on being able to read the arguments passed to an application and perform a task on those arguments.

Write a program that accepts the parameter "-addNumbers" as a command-line argument. The program will take all the arguments after the -addNumbers parameter, add them together and display the total to the user. The program must:
  • display the message "No command-line arguments have been entered!" to the user when no command-line arguments are entered.
  • be able to total numbers that include integers, real numbers and minus numbers.
  • display a message "The arguments you have entered are not all numbers."
  • ignore arguments entered in before the -addNumbers parameter.
  • if the arguments can be totaled then this must be displayed to the user (i.e., "The total value of the arguments is: ").

To get the most out of this question try and figure out the answer before coming back to read the solution on the next page.

  1. About.com
  2. Technology
  3. Java
  4. Programming Exercises
  5. Beginner Level
  6. Parsing Command-Line Arguments Question

©2014 About.com. All rights reserved.