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!)