user2135654
user2135654

Reputation: 91

How do I swap the places of an int in java?

I need to swap places of an int. For example, if I call for the method swapDigitPairs(482596), it will return me 845269. The 9 and 6 are swapped, as are the 2 and 5, and the 4 and 8.

If the number contains an odd number of digits, leave the leftmost digit in its original place. For example, the call of swapDigitPairs(1234567) would return 1325476. I'm not suppose to solve using a string and I should use a while loop to solve it. I'm not supposed to use any arrays.

Below is what I have done so far. But I am stuck at the swapping position and I know I need to multiply by the places(like tenth,thousand etc) depending on the number.But I am stuck at this part. What I have done is to retrieve the number one by one.

public static int swapDigitPairs(int number) {

    while(number!=0) {
        int firstDigit = number%10;

        for(int i =10;i<=;i*=10) {
            int secondDigit= firstDigit*i;
        }

        int leftOverDigit = number/10;

        number=leftOverDigit;

    }
    return number;
}

Upvotes: 2

Views: 6904

Answers (5)

user10782719
user10782719

Reputation:

I'm new to Java, but this might be another solution;

import java.util.Scanner;

public class DigitsPlaceSwap {

    public static void main(String[] args) {
        DigitsPlaceSwap demo = new DigitsPlaceSwap();

        System.out.print("Enter a whole number : ");
        Scanner stdin = new Scanner(System.in);
        int number = stdin.nextInt();
        
        demo.switchDigits( number );
    }

    public void switchDigits( int number ) {
        int getDigits = 0;
        int numberOfDigits = String.valueOf(number).length();
        String result = "";

        for( int i = 1; i <= numberOfDigits; i++) {
            getDigits = ( ( number % (int) Math.pow( 10, i ) ) / (int) Math.pow( 10, i - 1 ) );
            result += Integer.toString( getDigits );
        }

        System.out.println( "Swapped Number is : " + result );
    }
}

It doesn't validate the input if it's a positive whole number in the range or not. I convert it to a String to make it print zeros on the left after swapped.

Enter a whole number : 342325400
Swapped Number is : 004523243

Upvotes: 0

Bohemian
Bohemian

Reputation: 425458

Here's the solution (tested):

public static int swapDigitPairs(int number) {
    int result = 0;
    int place = 1;
    while (number > 9) {
        result += place * 10 * (number % 10);
        number /= 10;
        result += place * (number % 10);
        number /= 10;
        place *= 100;
    }
    return result + place * number;
}

The key points here are:

  • the loop consumes digits from the right hand side of the number, so the odd/even processing distinction is gracefully handled
  • the terminating condition is that there are at least two digits remaining
  • loop logic deals with two digits per iteration
  • use the remainder operator % with 10 (ie number % 10) to produce the last digit
  • integer division by 10 numerically truncates the last digit
  • there is no need to hold the last digit in a variable - it just clutters the code

Here's some tests and some edge cases:

public static void main(String[] args) throws Exception {
    System.out.println(swapDigitPairs(482596));
    System.out.println(swapDigitPairs(1234567));
    System.out.println(swapDigitPairs(12));
    System.out.println(swapDigitPairs(1));
    System.out.println(swapDigitPairs(0));
}

Output:

845269
1325476
21
1
0

Upvotes: 6

Abhishek Kumar
Abhishek Kumar

Reputation: 219

Here is the code with some test cases

public class ReverseNumberDigit {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(reverseNumber(123456789));
        System.out.println(reverseNumber(12345678));
        System.out.println(reverseNumber(1234567));
        System.out.println(reverseNumber(123456));
        System.out.println(reverseNumber(12345));
        System.out.println(reverseNumber(1234));
        System.out.println(reverseNumber(123));
        System.out.println(reverseNumber(12));
        System.out.println(reverseNumber(1));

    }

    private static int reverseNumber(int orinalNumber) {
        int reverseNumber = 0;
        int x = 1;

        while(orinalNumber > 9){
        int lastNumber = orinalNumber %  10; 
        orinalNumber = orinalNumber / 10;
        int secondNumber = orinalNumber %  10;
        orinalNumber = orinalNumber / 10;
        reverseNumber = reverseNumber + (lastNumber * x * 10) + (secondNumber * x);
        x = x * 100;
        }
        if(orinalNumber > 0){
            reverseNumber = reverseNumber + orinalNumber * x;
        }
        return reverseNumber;
    }

}

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

a simple solution is

    int i1 = 482596;
    char[] a = String.valueOf(i1).toCharArray();
    for (int i = 0; i < a.length - 1; i += 2) {
        char tmp = a[i];
        a[i] = a[i + 1];
        a[i + 1] = tmp;
    }
    int i2 = Integer.parseInt(new String(a));

Upvotes: 2

shuangwhywhy
shuangwhywhy

Reputation: 5625

If you have abcdef

Then ((a+b)*10000 + (c+d)*100 + (e+f)) * 11 - abcdef = badcfe

Upvotes: 2

Related Questions