Skip to content

Instantly share code, notes, and snippets.

@KingsleyUsoroeno
Created May 29, 2020 20:40
Show Gist options
  • Save KingsleyUsoroeno/2c7521a4f5407e079a54d61c6f3880fb to your computer and use it in GitHub Desktop.
Save KingsleyUsoroeno/2c7521a4f5407e079a54d61c6f3880fb to your computer and use it in GitHub Desktop.
A function that requires a two digit and determines if its the largest of two possible number swaps
private static boolean numberSwap(int number) {
try {
// first know if the number parse in is of two length
String s = String.valueOf(number);
if (s.length() < 2) {
System.out.println("number has to be of two length");
return false;
}
// reverse the number
String reservedNumber = new StringBuilder(String.valueOf(number)).reverse().toString();
// convert it back to an int[These can also be done with a one liner but these is much cleaner]
int num = Integer.parseInt(reservedNumber);
System.out.println("swapped number is " + num);
return number > num;
} catch (NumberFormatException e) {
System.out.println("number exception is " + e.getMessage());
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment