Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Write a Java program to swap two variables.

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program to swap two variables.

    Not sure where to post this question since its my first time on this forum. But I wanted to see if my code for swapping two variables is correct or if there is another way:

    I did mines two ways:

    First way (with known variables)
    Input:
    class RicMain  
     
    { 
     
        public static void main(String args[])  
     
        { 
     
            int x = 10; 
     
            int y = 20; 
     
     
     
            System.out.println("Before swapping"); 
     
            System.out.println("x = " + x); 
     
            System.out.println("y = " + y); 
     
     
     
            System.out.println("After swapping"); 
     
            System.out.println("x = " + y); 
     
            System.out.println("y = " + x); 
     
        }     
     
    }
    Second way(where the user has to input two variables)
    import java.util.Scanner; 
     
     
     
    class RicMain  
     
    { 
     
        public static void main(String args[])  
     
        { 
     
            Scanner swap = new Scanner(System.in); 
     
            int x, y; 
     
     
     
            System.out.println("Enter num for x: "); 
     
            x = swap.nextInt(); 
     
            System.out.println("Enter num for y: "); 
     
            y = swap.nextInt(); 
     
     
     
            System.out.println("Before swap"); 
     
            System.out.println("x = " + x); 
     
            System.out.println("y = " + y); 
     
     
     
            System.out.println("After swap"); 
     
            System.out.println("x = " + y); 
     
            System.out.println("y = " + x); 
     
        }     
     
    }

    Are these good?
    Last edited by Norm; April 4th, 2018 at 11:38 AM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a Java program to swap two variables.

    Can you define what you mean by "swap variables"?
    Normally I understand swap to mean that the contents of the variables are swapped. For example:
    Before a swap:
    x=10
    y=20
    after the swap
    x=20
    y=10

    Your code does not swap the contents of the variables. It swaps the order that their contents are printed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to swap two variables.

    By "swapping variables", I mean exchanging the values of the variables. I don't know the exact method to swap two variables, these codes are the best thing I can do.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a Java program to swap two variables.

    What if you have an egg in each hand and you want to swap the eggs? How would you do it? A hand can only hold one egg at a time. There is another person there that could help.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to swap two variables.

    Quote Originally Posted by Norm View Post
    What if you have an egg in each hand and you want to swap the eggs? How would you do it? A hand can only hold one egg at a time. There is another person there that could help.
    This is how I would think about it (maybe its not the right way of thinking, maybe it is, I don't know)
    Say you have two eggs: one egg in your left hand, the other egg in your right hand. Say you want to swap those eggs, but you hand can only hold one egg at a time. In order to swap the eggs, you put one egg(the one in your right hand) on the counter, then transfer the egg in your left hand to your right hand, then put the egg you placed on the counter in your right hand.
    Does that make any sense?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a Java program to swap two variables.

    you put one egg(the one in your right hand) on the counter,
    Yes that is how I would do it. In programming, "on the counter" would mean using another variable (often named temp) to hold the value during the swap.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to swap two variables.

    So now I rewrote my code. How does it look?

    Input:
    class RicMain  
    { 
    	public static void main(String args[])  
        { 
            int x = 10; 
            int y = 20; 
            int z; //z is the "holding" variable
     
            System.out.println("Before swapping"); 
            System.out.println("x = " + x); 
            System.out.println("y = " + y); 
     
            z = x; //z holds 10
            x = y; // y now has 20
            y = z; // y now has 10
            System.out.println("After swapping");
            System.out.println("x = " + x);
            System.out.println("y = " + y);
        }     
    }
    Last edited by Norm; April 4th, 2018 at 04:15 PM. Reason: Added code tags.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a Java program to swap two variables.

    Do you get the expected output?
    One change: use the name temp instead of z so anyone looking at the code understands what the variable is used for.
    x,y,z look like 3 variables that are meant to hold values.

    Please be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to swap two variables.

    You can do it without a temp var.
    class Main {
      public static void main(String[] args) {
     
        double a = 5.6;
        double b = -4.9;
        System.out.println("a: " + a);
        System.out.println("b: " + b);
     
        a= a+b;
        b= a-b;
        a= a-b;
     
        System.out.println("a: " + a);
        System.out.println("b: " + b);
     
      }
    }

Similar Threads

  1. write a java program
    By mohd faizan shekh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2013, 05:31 PM
  2. Replies: 2
    Last Post: January 25th, 2013, 09:54 PM
  3. write a program in java?
    By rajasekharreddy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2012, 09:58 AM
  4. help me write java program
    By stevisto in forum Member Introductions
    Replies: 1
    Last Post: September 13th, 2011, 03:44 PM