Sum of two numbers using command line arguments in java

Command line argument is as a way to pass arguments in java while running java program.

  • Sum of two integer numbers using command line arguments in java
  • Sum of two double numbers using command line arguments in java
  • Conversion from String to number using Wrapper class
  • How to check length of command line argument in Addition of two numbers
  • Exception handling with command line arguments while adding two numbers

1 Sum of two integer numbers using command line arguments in java

Steps to add two integer numbers is below

  1. Read command-line variable array args[0] and args[1].
  2. Convert it to integer value and store it in two variables.
  3. add both variables and store in another variable sum
  4. print the sum.

Run above program from command prompt

>javac CommandLineArguments.java

>java CommandLineArguments 8 5

command line arguments in java
Fig: command line arguments in java

Output

value 8 will pass to args[0] and value 5 will store in args[1].

As we know args is String array.

So to convert value from string we used Integer.parseInt() method.

Lets see another example

2 Sum of two double numbers using command line arguments in java

Output

Similar way we can add long, float values.

You have to know how to convert string to long and string to float and from string to other data types.

This is possible with the use of Java wrapper classes.

Each primitive data type has its own class that is known as wrapper class.

Wrapper class contain methods to convert value from string to specify object type.

Following Classes are sub class of Number class.

You can find details in the Number class also check all its sub-classes.

Number Class in Java
Number Class in Java

3 Conversion from String to number using Wrapper class

Sr NoConvert from string toMethod
1ByteByte.ParseByte()
2ShortShort.valueOf()
3IntegerInteger.shortInt()
4LongLong.parseLong()
5FloatFloat.parseFloat()
6DoubleDouble.parseDouble()

Above programs are very simple.

Lets assume a situation if use does not provide two values or provides values like alphabet then how to deal with it.

4 How to check length of command line argument in Addition of two numbers

In addition, we should check whether the user has provided two numbers or not.

Let’s check it.

Output

args is string array so we can use the length property to check its length.

if (args.length != 2) then we are showing a message to the user that Please provide two arguments and terminate the execution.

5 Exception handling with command line arguments while adding two numbers

A. Handling ArrayIndexOutOfBoundsException

This exception occurs because we tried to access an array index that does not exist.

Read More

Exception Handling try catch finally blocks in Java
Exception Handling in Java: Hierarchy Example and Types
User-Defined (Custom) Exceptions in Java with Examples

If the user passed only one value and we access the first two values then this error can occur.

To avoid this we have already seen the above validation problem.

Lets again run our first program and pass only one value

Output

Let’s see how to handle it with the exception

Output

Result

B. Handling NumberFormatException in Command line addition of numbers

This exception occurs when the method is unable to convert string to a number type.

For example, a user gives “w” as input so the method is unable to convert it to a number.

Then this method will throw NumberFormatException.

Output

To resolve NumberFormatException again we will use try catch block.

Output

C. Handling Multiple Exceptions from command line input

try can throw multiple exceptions to handle this we can create multiple catch blocks each for a specific exception.

We can also use one catch block to handle multiple exceptions.

Read More

  1. Duplicate Number between 1 to n numbers
  2. Print vowels in a String
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Square Star Pattern

1 thought on “Sum of two numbers using command line arguments in java”

Comments are closed.