Home Java Learn-java-in-a-day Add Two Numbers in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Add Two Numbers in Java

Advertisement
In this section, you will learn to work with command prompt arguments provided by the user. We will access these arguments and print the addition of those numbers. In this example, args is an array of String objects that takes values provided in command

Add Two Numbers in Java

     

In this section, you will learn to work with command prompt arguments provided by the user. We will access these arguments and print the addition of those numbers. In this example, args is an array of String objects that takes values provided in command prompt. These passed arguments are of String types so these can't be added as numbers. So to add, you have to convert these Strings in numeric type (int, in this example). Integer.parseInt helps you to convert the String type value into integer type. Now you can add these values into a sum variable and print it on the console by println() function.

Here is the code of program:

public class AddNumbers{
  public static void main(String[] args) {
  System.out.println("Addition of two numbers!");
  int a = Integer.parseInt(args[0]);
  int b = Integer.parseInt(args[1]);
  int sum = a + b;
  System.out.println("Sum: " + sum);
  }
}

Download this example.

Output of program:

C:\vinod>javac AddNumbers.java

C:\vinod>java AddNumbers 12 20
Addition of two numbers!

Sum: 32
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 25, 2008

Ask Questions?    Discuss: Add Two Numbers in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
praveen
March 28, 2011
problem in compiling

class AddNumber is public, should be declared in a file name AddNumber.java
Ravi Punia
April 10, 2011
Question of Java

How to muliply two big numbers of approximatly 200 digits in java????tell me if u have a mind...challenge
ajay
April 24, 2011
java software

hello,geneious i want to a java software for java programming which is "jdk1.6.10_24"...... so please give me its free download link...where i can direct download this software.....
Joey
May 26, 2011
Code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Midterm Exam Part 2</title> <script language="JavaScript" type="text/javascript"> function addNum(num1, num2){ num3 = num1 + num2; document.write("<p>The answer is "+num3+".</p>"); } function getNum(num1, num2){ document.write(num1+num2); addNum(num1, num2); } </script> </head> <center> <br/> <br/> <br/> <br/> <body> <font color="blue">Mid Term Part 2 <BODY BGCOLOR="Grey"> <form action="" name="form1" id="form1"> <br/><br/> <input type="text" name="num1"/> + <input type="text" name="num2"/> =<br/> <br/> <input type="button" name="submit" value="Submit" onclick="getNum(document.form1.num1.value,document.form1.num2.value)"/> </form> </body> </html>
Olayemi oluwadurotimi
June 23, 2011
Computer programming

Format on how write a program to compute for processing and grading a students result p.s with explanations on how it should work
musembi
July 11, 2011
a vb code

its workin well, thanks to Rose India Technolgy
shilpa
July 1, 2011
java

Firstly,I think that the JAVA is very hard languese but when i read this program i feel it is very easy for my study.Thanks a lot.
vishal
July 29, 2011
error in program

it shows the error like: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at AddNumbers.main(AddNumbers.java:6)
elavarasan
September 26, 2011
ask

how to add two numbers using only one variable in java
dado
September 29, 2011
none

tnx 4 d info.....
Sumit
November 3, 2011
Java simple program error !

I am getting this error, what does this mean ? sum2.java:1: class AddNumbers is public, should be declared in a file named AddN umbers.java public class AddNumbers{
tej jadhav
December 3, 2011
java(jsp)

your program in not match the argument
Nilay
December 11, 2011
Error when executed

when i try running this program, it gives error. the error says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at sum.main(sum.java:4) i have no idea what it means, m a rookie. Please help!!!! Reply as soon as possible!
Ravi
December 28, 2011
java

thank's
nicitegetse oscar
January 18, 2013
java

java is not making things clear as c++ language
sneha
July 9, 2012
about comment

nice one
hello
July 27, 2012
to

hello
Neha Tanwar
July 28, 2012
programme

im seeing that
Abhishek
August 1, 2012
query

public class AddNumbers{ public static void main(String[] args) { System.out.println("Addition of two numbers!"); int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; System.out.println("Sum: " + sum); } } this program give the ArrayIndexOutOfBoundException....please help...
tarun kumar
August 4, 2012
java program for addition of two numbers

excelent
xxx
August 23, 2012
java

easy to understood
krish
September 6, 2012
Exception

This program could not compile in Eclipse IDE
Vaibhav
September 8, 2012
Not able to run java program..getting error

Hi, Below is my program...I've just started learning java and I am getting errors in this and not able to compile..pls help. public class Addition { int numOne,numTwo; public void claculateNum() { numOne=10; numTwo=20; calculation=numOne+numTwo; System.out.println("Inside first method"); System.out.println("Sum is : "+calculation); } public void claculateAgain() { numOne=20; numTwo=20; calculation=numOne+numTwo; System.out.println("Inside second method"); System.out.println("Sum is : "+calculation); } public static void main(String args[]) { Addition add = new Addition(); add.calculateNum; add.calculateAgain; } }
khader
September 28, 2012
cs 211

thank's for helping to all of people
Vijay
September 28, 2012
Pgm

Thanks
Ambuj kumar
October 2, 2012
java

nice
Carla
October 14, 2012
Add 2 Numbers in Java

Tried the above code but it keeps on giving the following error....Exception in thread "main" java.lang.arrayindexoutofboundsexception:0 (error is the line below system.out.println.... Does anyone know how to fix this... Thanks.
Jericoholic
October 21, 2012
Another Way to Add 2 Numbers

import java.util.Scanner; public class AddNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int a; int b; int sum; System.out.print("Enter First Number:"); a = input.nextInt(); System.out.print("Enter Second Number:"); b = input.nextInt(); sum = a + b; System.out.print("Sum is "); System.out.println(sum); } }
AYSUN
December 23, 2012
nice soluion

Thank you this is the most convenient solution that i have seen.
anurag
July 10, 2013
java

thanks for provide to facility
jewel
June 4, 2014
java

problem all java program
uma
April 11, 2014
java

int a=Integer.parseInt(args[]) to show an error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
DMCA.com