• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Addition of Two Numbers in Java

May 17, 2020 by Admin Leave a Comment

Contents

  • 1 Addition of two numbers using Scanner
  • 2 Addition of two numbers using BufferedReader
  • 3 Addition of two numbers using Command Line Arguments

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

Filed Under: Java

Previous Post: « Finding Maximum of Three Numbers in Java
Next Post: Average of n Numbers in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in