Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Add two numbers

Problem : Write a program that adds two arguments that passed through command line arguments?

Program :

Step 1 - Coding

Create a text file c:\sunilos\Add.java  and copy below contents.

/*

 * A program which take two integer from command line argument and add.
 */


public class Add {

    public static void main(String[] args) {

        int a = Integer.parseInt(args[0]); //Integer.parseInt method converts String data type into Integer
        int b = Integer.parseInt(args[1]);

        int sum = a + b;

        System.out.println("Sum is " + sum);
    }
}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy Add.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile Add.java with help of 
    • javac Add.java command.  Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java Add 5 9 command.

Output


It will display result
Sum is 14


<<Previous | Next>>
ċ
Add.java
(1k)
nakul shukla,
Mar 19, 2009, 7:11 AM
Comments