Addition of Two Numbers in Java

We will solve this basic problem of the addition of two numbers in three ways, first by using a Scanner, second by using BufferedReader, and at last by using command-line arguments.

Addition of two numbers using Scanner

Java provides Scanner class in java.util package. This class is very popular to read values from the user as it allows the user to pass InputStream as an argument. So, in our example we are going to pass System.in which is the standard input stream.

Scanner also provides various methods to read specific datatypes like nextInt(), nextFloat(), nextDouble(), nextByte(), nextBoolean(), nextLong() and nextLine() etc.,

You should make sure to call the close method of the Scanner once the required operation is performed.

package com.techstackjournal;

import java.io.IOException;
import java.util.Scanner;

public class AddTwoNumbers {

	public static void main(String[] args) throws IOException {

		Scanner scanner = new Scanner(System.in);
		int num1, num2;

		System.out.print("Enter value for num1:");
		num1 = scanner.nextInt();
		scanner.nextLine();
		
		System.out.print("Enter value for num2:");
		num2 = scanner.nextInt();
		scanner.close();

		System.out.println("Addition of num1 and num2 is "+ (num1 + num2));

	}

}
Enter value for num1:2
Enter value for num2:4
Addition of num1 and num2 is 6

Addition of two numbers using BufferedReader

In this example, we will pipe InputStreamReader into BufferedReader. Both these classes are available in java.io package. InputStreamReader constructor accepts InputStream, so we can pass the standard input stream (System.in) into it.

Similar to Scanner, BufferedReader also provides datatype specific methods to read values from input stream.

package com.techstackjournal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AdditionWithBufferedReader {

	public static void main(String[] args) throws IOException {

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("Enter value for num1:");
		int num1 = Integer.parseInt(reader.readLine());

		System.out.print("Enter value for num2:");
		int num2 = Integer.parseInt(reader.readLine());
		reader.close();

		System.out.println("Addition of num1 and num2 is : " + (num1 + num2));

	}

}
Enter value for num1:10
Enter value for num2:20
Addition of num1 and num2 is : 30

Addition of two numbers using Command Line Arguments

In this example, we are going to pass the required values to the program via the command-line. Command-line arguments will be available in the String array variable of main method.

package com.techstackjournal;

import java.io.IOException;

public class AdditionCommandLine {

	public static void main(String[] args) throws IOException {

		int num1 = Integer.parseInt(args[0]);
		int num2 = Integer.parseInt(args[1]);

		System.out.println("Addition of num1 and num2 is : " + (num1 + num2));

	}

}
  • To run this program on Eclipse
    • Right click and select Run As >> Run Configurations…
    • Click on “New Launch Configuration” icon
    • Select Arguments tab
    • Type 2 numbers separated by space in “Program arguments” text box
    • Click on Run button
Passing Command-Line Arguments in Eclipse
Addition of num1 and num2 is : 30

Leave a Comment