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.

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.

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

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().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.