Java Swap Values

This program swaps the values of two inputs and then prints them back out to the console.

package com.thealmostengineer.swapvalues;

import java.util.Scanner;

public class Swapper {

    private static int x; 
    private static int y;

     * @param args
    public static void main(String[] args) {

        System.out.println("Enter two integer values: ");
        Scanner sc = new Scanner(;
        x = sc.nextInt();
        y = sc.nextInt();

        swapValues(x, y);


    public static void systemPause(){
        System.out.println("Press any key to continue...");
        new Scanner(;

    private static void swapValues(int x, int y){
        int temp;

        System.out.println("Original values: " + x + " " + y);
        temp = x;
        x = y; 
        y = temp; 
        System.out.println("Values swapped: " + x + " " + y);

I enjoy sharing the content that you find here. I ask that you consider purchasing an item on my Amazon Wish List or you can make a donation.

Back To Top