Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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

 

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

                         

» View all related tutorials
Related Tags: java c http tools com jsp orm date development ui form time update io download load version install test compile

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

11 comments so far (
post your own) View All Comments Latest 10 Comments:

It was simple and clear and explanatory.

Posted by Sann on Monday, 04.6.09 @ 19:50pm | #86563

A very very good website for learning any technology.

I want to appreciate every one in your team whole heartly for their extra-ordinary work.

Keep up the good work.

Posted by G Ravi Kumar on Saturday, 04.4.09 @ 12:09pm | #86494

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);
}
}

Posted by harish on Thursday, 12.4.08 @ 00:58am | #82356

your programm is very fine.

Posted by bhism pratap singh on Monday, 10.6.08 @ 11:22am | #80919

Addition of two numbers!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at addnumbers.main(addnumbers.java:4)


why do that ,,must print the sum when add two number

Posted by bolbol on Monday, 09.22.08 @ 15:05pm | #80597

Hii, cant we just pass a numeric array ?

like public static void main (int[] numarry)
.........
....
...
...
}

Posted by Jengo on Tuesday, 06.3.08 @ 13:17pm | #61935

public static void main(String[] args) {
int a = 5;
int b = 7;
System.out.println("Addition of two numbers!");
int sum = a + b ;
System.out.print("Sum is: "+sum);
}
}

Posted by khalid on Tuesday, 03.11.08 @ 14:55pm | #52283

it happens wen u havent passed the arguments..

at run time,entere like java AddNumbers 10 20 and not just javaAddNumbers

Posted by Anand on Wednesday, 11.28.07 @ 16:35pm | #40828

arter compiling o/p is error: cannot read: Add.java
and at run time:
Exception in thread "main" java.lang.NoClassDefFoundError: Add

Posted by srinivas on Saturday, 10.20.07 @ 15:37pm | #34447

Using jdk 1.6 this code returns.

Addition of two numbers!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at addnumbers.main(addnumbers.java:4)

Posted by Leadgolem on Saturday, 06.9.07 @ 11:00am | #18695

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.