Created
July 9, 2013 15:53
-
-
Save six519/5958511 to your computer and use it in GitHub Desktop.
Reverse Binary (Spotify Puzzle in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
class ReversedBinaryToDecimal { | |
public static void main(String args[]) { | |
Scanner scanner = new Scanner(System.in); | |
int decimalNumber; | |
try { | |
decimalNumber = scanner.nextInt(); | |
if(decimalNumber >= 1 && decimalNumber <= 1000000000) { | |
System.out.println(Integer.parseInt(new StringBuilder(Integer.toBinaryString(decimalNumber)).reverse().toString(),2)); | |
} else { | |
throw new InputMismatchException(); | |
} | |
}catch(InputMismatchException e) { | |
System.out.println("Invalid input"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment