Okay, last question of the night.

My program is to have a program collect a number, double it, and reverse. I am having problems with reversing it...

Here is my code:

``````import java.util.Scanner;
import java.text.DecimalFormat;
public class ReverseNumber {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
long input = s.nextLong();
long result = reverse(input);
DecimalFormat myFormatter = new DecimalFormat("#,##0");
System.out.println(myFormatter.format(input) + " doubled is " + myFormatter.format(input*2) + " and then reversed is " + myFormatter.format(reverse(result)));
}

public static long reverse(long n) {
long result = 0;
long rem;
while (n > 0) {
rem = n % 10;
n = n / 10;
result = result * 10 + rem;
}
return result;
}
}
``````

Here is the error log (1):

``````First Difference Occurs at: byte 75, line 1
On Line 1 its column 75
Line 1 Wrong: Enter a positive integer: 240,340 doubled is 480,680 and then reversed is 24,034
Line 1 Right: Enter a positive integer: 240,340 doubled is 480,680 and then reversed is 86,084
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter a positive integer: 240,340 doubled is 480,680 and then | Enter a positive integer: 240,340 doubled is 480,680 and then

Octal Dump Difference
0000100   v   e   r   s   e   d       i   s       2   4   ,   | 0000100   v   e   r   s   e   d       i   s       8   6   ,

Octal Dump Your output...
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       2   4   0   ,   3   4
0000040   0       d   o   u   b   l   e   d       i   s       4   8   0
0000060   ,   6   8   0       a   n   d       t   h   e   n       r   e
0000100   v   e   r   s   e   d       i   s       2   4   ,   0   3   4
0000120  \n
0000121

Octal Dump Solution..
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       2   4   0   ,   3   4
0000040   0       d   o   u   b   l   e   d       i   s       4   8   0
0000060   ,   6   8   0       a   n   d       t   h   e   n       r   e
0000100   v   e   r   s   e   d       i   s       8   6   ,   0   8   4
0000120  \n
0000121
``````

Error log 2:

``````First Difference Occurs at: byte 87, line 1
On Line 1 its column 87
Line 1 Wrong: Enter a positive integer: 2,147,483,647 doubled is 4,294,967,294 and then reversed is 2,147,483,647
Line 1 Right: Enter a positive integer: 2,147,483,647 doubled is 4,294,967,294 and then reversed is 4,927,694,924
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter a positive integer: 2,147,483,647 doubled is 4,294,967, | Enter a positive integer: 2,147,483,647 doubled is 4,294,967,

Octal Dump Difference
0000120   e   d       i   s       2   ,   1   4   7   ,   4   | 0000120   e   d       i   s       4   ,   9   2   7   ,   6
0000140   6   4   7  \n                       | 0000140   9   2   4  \n

Octal Dump Your output...
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       2   ,   1   4   7   ,
0000040   4   8   3   ,   6   4   7       d   o   u   b   l   e   d
0000060   i   s       4   ,   2   9   4   ,   9   6   7   ,   2   9   4
0000100       a   n   d       t   h   e   n       r   e   v   e   r   s
0000120   e   d       i   s       2   ,   1   4   7   ,   4   8   3   ,
0000140   6   4   7  \n
0000144

Octal Dump Solution..
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       2   ,   1   4   7   ,
0000040   4   8   3   ,   6   4   7       d   o   u   b   l   e   d
0000060   i   s       4   ,   2   9   4   ,   9   6   7   ,   2   9   4
0000100       a   n   d       t   h   e   n       r   e   v   e   r   s
0000120   e   d       i   s       4   ,   9   2   7   ,   6   9   4   ,
0000140   9   2   4  \n
0000144
``````

Error Log 3:

``````First Difference Occurs at: byte 82, line 1
On Line 1 its column 82
Line 1 Wrong: Enter a positive integer: 52,010,023 doubled is 104,020,046 and then reversed is 52,010,023
Line 1 Right: Enter a positive integer: 52,010,023 doubled is 104,020,046 and then reversed is 640,020,401
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter a positive integer: 52,010,023 doubled is 104,020,046 a | Enter a positive integer: 52,010,023 doubled is 104,020,046 a

Octal Dump Difference
0000120       5   2   ,   0   1   0   ,   0   2   3  \n       | 0000120       6   4   0   ,   0   2   0   ,   4   0   1  \n
0000134                               | 0000135

Octal Dump Your output...
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       5   2   ,   0   1   0
0000040   ,   0   2   3       d   o   u   b   l   e   d       i   s
0000060   1   0   4   ,   0   2   0   ,   0   4   6       a   n   d
0000100   t   h   e   n       r   e   v   e   r   s   e   d       i   s
0000120       5   2   ,   0   1   0   ,   0   2   3  \n
0000134

Octal Dump Solution..
0000000   E   n   t   e   r       a       p   o   s   i   t   i   v   e
0000020       i   n   t   e   g   e   r   :       5   2   ,   0   1   0
0000040   ,   0   2   3       d   o   u   b   l   e   d       i   s
0000060   1   0   4   ,   0   2   0   ,   0   4   6       a   n   d
0000100   t   h   e   n       r   e   v   e   r   s   e   d       i   s
0000120       6   4   0   ,   0   2   0   ,   4   0   1  \n
0000135
``````

Any clues on why I can't get it to reverse properly?

Did i make a mistake in my while loop?

Thanks (last question of the night, i promise!)

## Recommended Answers

a "quick fix" to reverse a value, is to turn it into a String, and reverse it's characters. after that, parse it back to a double.
you'll save yourself quite some logic/work.

## All 4 Replies

a "quick fix" to reverse a value, is to turn it into a String, and reverse it's characters. after that, parse it back to a double.
you'll save yourself quite some logic/work.

You also seem to be reversing the original number, whereas the "right" answer reverses the doubled value.

:-) convert to String, hmmm then how to do, something

• with decimal and thoushand (I'm hope that there isn't added or inputed Indian Number Format, inputed by mistake, then all logics go away, to milky way) separator(s)

@<M/> this lesson has important issue because you determine nextLong(), then you have two choices

• a.m. issue with getAnyNumberInstance about/with decimal and thoushand separator(s)
• or don't thinking about and reverse order by put decimal and thoushand separator to correct possition in arrays of Chars (e.i.)

Actually the problem in your code is that you have reversed `input` twice!

At line 9 you have called the method `reverse(input);` and at this point the return value of the method is the reversed value of the input. Let's say input is equal to 1234 so the `reverse(input)` would be 4321. So at line 9:

``````//input = 1234
long result = reverse(input); //here result=4321
``````

Now you have called `reverse` method again at line 11 like this : `+ myFormatter.format(reverse(result)))`. Keep in mind result before this was 4321 and clearly reversed! so you did not have to call reverse again. At line 11 you reversed `result` so now the output will eventually be the original input. To change this just remove the method call and just output the `result`.

``````System.out.println(myFormatter.format(input) + " doubled is " + myFormatter.format(input*2) + " and then reversed is " + myFormatter.format(result));
``````

^That was your aim right? To reverse the 'original' input and not the doubled value?

Be a part of the DaniWeb community

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