Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Learn-java-in-a-day Add Two Numbers in Java

Related Tutorials


 
 

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
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Add Two Numbers in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:32
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