Swapping last two numbers giving ArrayIndexOutOfBoundException... Please correct me where I am doing wrong as I am new to programming and not able to find the problem... Thank you in advance...
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the Number : ");
    int n = scan.nextInt();
    int m = n;
    int size = String.valueOf(n).length();
    int arr[] = new int[size];
    for (int i = 0; i < size; i++) {
        arr[i] = n % 10;
        n = n / 10;
    }
    System.out.println("The next permuatation will be : ");
    int temp = arr[m - 2];
    arr[m - 2] = arr[m - 1];
    arr[m - 1] = temp;
    System.out.print(arr);
}
