0

I am writing a program to calculate a numeric palindrome, and to do that i have to reverse the digits of a number, and add that to the original.

The best way i can think of to reverse the number would be to put each digit individually in an array, reverse the digits, then put it back together as an int. My problem is putting each digit of a number into an array in a spot by themselves. Any help you could give, even just telling me what this is called so i can research it, would be greatly appreciated.

3
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by server_crash
0

I would put them into a String, create a String Buffer and then reverse it:


int x = 123;
String s = x+"";
StringBuffer sb = new StringBuffer(s);
sb.reverse();


That reverses the String, then you can parse it back to an int. I do have a recursive solution to this, but I would stay away from it.

0

I generally use x.toString, but x + "" would work also.

Mike, by now you should know that I am lazy. Typing x+"" is less work than typing x.toString().

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.