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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Swap two any numbers (from Keyboard) 
 

This is a simple Java Oriented language program. If you are newbie in Java programming then our tutorial and example are helpful for understanding Java programming in the simplest way.

 

Swap two any numbers (from Keyboard)

                         

This is a simple Java Oriented  language program. If  you are newbie in Java programming then our tutorial and example are helpful for understanding Java programming in the simplest way.  Here we will learn how to swap or exchange the number to each other. First of all we have to create a class "Swap". Now, we use the Integer.parseInt(args[o]) and Integer.parseInt(args[1]) methods for getting the integer type values in command line. Use a temporary variable z of type integer that will help us to swap the numbers  Assign the value of x to z variable, now assign the value of y to the variable x. Lastly assign the value of z to variable y. Now we get the values has been interchanged. To display the values on the command prompt use println() method and the swapped values will be displayed.

Here is the code of this program

public class Swap {
  public static void main(String[] args) {
  
    int x= Integer.parseInt(args[0]);
    int y= Integer.parseInt(args[1]);
    System.out.println("Initial value: "+ x +" and " + y);
    int z = x;
    x = y;
    y = z;
    System.out.println("Swapped value: " + x +" and " + y);
  }
}

Download this Example. 

                         

» View all related tutorials
Related Tags: c file class orm constructor object path struct const value name return instance paths create if send match to bot

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

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

Hello i had a problem with the SWAP program. When I ran it gave me an error.

This is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Swap.main(Swap.java:4)

Posted by Thabisa on Saturday, 07.11.09 @ 16:06pm | #89243

hey thanks for this!

Posted by dinho on Thursday, 03.15.07 @ 07:03am | #11749

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.