Reputation: 331
I want to reverse an integer. I have trying by the following code, the problems occurs when the number is so large that its reverse gets larger than the int limit (for example 1534236469), so in that case it returns some garbage integer.
How can I make use of Integer.MIN_VALUE
and Integer.MAX_VALUE
to check if the reversed number is within the limits?
Note: it must use int
variable type only.
int num = 1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
//get the last digit
int digit = num % 10;
//multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
multiplier = 10;
num /= 10;
}
//how to fix this.
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
System.out.println("Reversed integer is " + reverseInt);
}
Upvotes: 2
Views: 1186
Reputation: 3166
You just have to make the check before you add the next digit, so you can simply move your check up to the actual calculation. If your current reversed positive number is greater than Integer.MAX_VALUE/10
, then you can't add another digit. Likewise with negative numbers.
The only thing I did was move this part of your code up:
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
then I added the /10
and a return
statement, since the program must end when there's an overflow:
public class StackOverflowTest {
public static void main(String[] args) {
int num = -1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
// if this next step will push is into overflow, then stop:
if (reverseInt < Integer.MIN_VALUE/10 || reverseInt > Integer.MAX_VALUE/10) {
System.out.println("Invalid number");
return;
} else {
//get the last digit
int digit = num % 10;
// multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
}
multiplier = 10;
num /= 10;
}
System.out.println("Reversed integer is " + reverseInt);
}
}
Alternatively you can treat it as a String, and then simply reverse the String (along with fiddling with the sign):
public class StackOverflowTest {
public static void main(String[] args) {
reverse(1534236469);
System.out.println();
reverse(-153423646);
}
public static void reverse(int num) {
System.out.println("int = " + num);
int number = num < 0 ? -num : num; // remove the sign
String reverse = new StringBuilder(String.valueOf(number)).reverse().toString();
System.out.println("reverse String: " + reverse);
try {
int reversed = Integer.parseInt(reverse);
reversed = num < 0 ? -reversed : reversed; // get back the sign
System.out.println("Reversed integer is " + reversed);
} catch (NumberFormatException ex) {
System.out.println("Invalid number");
}
}
}
Prints:
int = 1534236469
reverse String: 9646324351
Invalid number
int = -153423646
reverse String: 646324351
Reversed integer is -646324351
Upvotes: 8
Reputation: 79075
Whenever the value of Integer.MAX_VALUE
is exceeded, it starts from Integer.MIN_VALUE
i.e. Integer.MAX_VALUE + 1
evaluates to Integer.MIN_VALUE
. You can use this feature to get the solution you are looking for.
Simply multiply reverseInt
in a loop from 1
to 10
and if the value becomes negative, it means that the next value (reverseInt * multiplier
) will exceed Integer.MAX_VALUE
. Also, you need to add reverseInt * multiplier
in a loop to a value from 0
to 9
and if the value becomes negative, it means that the next value (reverseInt * multiplier + digit
) will exceed Integer.MAX_VALUE
.
public class Main {
public static void main(String[] args) throws InterruptedException {
int num = 1534236469;
boolean valid = true;
int reverseInt = 0;
int multiplier = 1;
if (num < 0) {
multiplier *= -1;
}
while (num != 0) {
// get the last digit
int digit = num % 10;
for (int i = 1; i <= multiplier; i++) {
for (int j = 0; j <= 9; j++) {
if (reverseInt * i < 0 || (reverseInt * i + j) < 0) {
System.out.println("Invalid number");
valid = false;
break;
}
}
if (!valid) {
break;
}
}
if (!valid) {
break;
}
// multiply the reverseInt by 10 and then add the last digit
reverseInt = reverseInt * multiplier + digit;
multiplier = 10;
num /= 10;
}
if (valid) {
System.out.println("Reversed integer is " + reverseInt);
}
}
}
Output:
Invalid number
Upvotes: 1
Reputation: 159086
In Java 8+, change to:
reverseInt = Math.addExact(Math.multiplyExact(reverseInt, multiplier), digit);
The code will now throw ArithmeticException
if the result overflows.
Upvotes: 2