2

My code below is supposed to accept an integer from the user, and then print whatever integer they enter in reverse order. I am getting no errors, but when I run this program the integer is not being printed in reverse. Can someone please help me figure out why?

import java.util.*;

public class ReverseDigits {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String response;

        System.out.println("Please enter a whole number.");
        response = sc.next();

        reverseDigit(response);
    }

    public static void reverseDigit(String digit) {
        ArrayList al = new ArrayList();
        al.add(digit);
        Collections.reverse(al);
        System.out.println("Your number in reverse order is: " + al);
    }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
carchelhf
  • 51
  • 1
  • 3

5 Answers5

6

You misinterpreted what Collections.reverse does. This method reverses the list that you gave, not its content. Since you called this method with a list having a single element, the result will be the same list.

If you want to reverse a String, please refer to this question.

As a side-note: do not use raw types like ArrayList, this will get into trouble. Prefer the type-safe way ArrayList<String>.

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

Try this code.

ArrayList al = new ArrayList();
al.add(new StringBuffer(digit).reverse().toString());
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

If you have a collection of one, it is the same in reverse order as forward.

If you want to reverse the string representation of an integer, you can use StringBuilder to reverse the digits.

StringBuilder sb = new StringBuilder();
sb.append(digits);
sb.reverse();
System.out.println("Your number in reverse order is: "+ sb);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

If you want to reverse a String why are you adding it to a list ?

Use this :

         String s = sc.next();
         StringBuilder sb = new StringBuilder(s);
         System.out.println(sb.reverse());
Rahman
  • 3,755
  • 3
  • 26
  • 43
0

The reverse(List<?>) method is used to reverse the order of the elements in the specified list. Since there is only one item in your collection, reverse order is same as the initial list. You can use the below code as you are trying to reverse an integer.

package com.stackoverflow.answer;

import java.util.*;

public class ReverseDigits {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter a whole number: ");
        int number = scanner.nextInt();
        System.out.println(String.format("Your number in reverse order is: %d", reverse(number)));
        scanner.close();
    }

    public static int reverse(int x) {
        return reverse(x, 0);
    }

    private static int reverse(int x, int y) {
        return x == 0 ? y : reverse(x / 10, y * 10 + x % 10);
    }

}
1218985
  • 7,531
  • 2
  • 25
  • 31